Tuesday, August 25, 2009

Comma separated column values in sqlserver

If there is any requirement arieses we need to show comma separated column values eg:
Dummy values (Requirement)
abc,xyz,pqr etc.
Database column values
fields have an id
CatId CatName
1              abc
2              xyz
3              pqr

To achieve above requirement use following code:
DECLARE @Cat varchar(2000)Select @Cat = COALESCE(@Cat + ', ', '') + CAST(SubCategoryName AS varchar(200))FROM SubCategoryWHERE CategoryId=@CategoryIDReturn @Cat

Friday, July 24, 2009

File downloading when button is in update panel

Following is the normal code for file downloading. But it will not work if button is placed inside update panel.
Syntax(C#):
//string FileResponse="";
string fileName = Request.QueryString["Region"];

string PdfFilePath = "";
PdfFilePath = Server.MapPath("Contracts/" + fileName);
//PdfFilePath = "http://www.thsweb.com/contracts/" + HiddenField2.Value;
if (File.Exists(PdfFilePath))
{
FileInfo fileInfoObj = new FileInfo(PdfFilePath);
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" +fileName);
Response.AddHeader("Content-Length", fileInfoObj.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(fileInfoObj.FullName);
//Response.Write(fileInfoObj.Name);
Response.End();
}

To make it working perfectly we need to add some javascript code on that page where our button to download the file is present.
Following is the code for javascript:
Syntax(Javascript):
function InitializeRequest() {
// Create an IFRAME.
var iframe = document.createElement("iframe");
// Get the desired region from the dropdown.
var region = document.getElementById('clientid of control that contains file name').value;
// Point the IFRAME to GenerateFile, with the
// desired filename as a querystring argument.
iframe.src = "GenerateFile.aspx?region=" + region;
// This makes the IFRAME invisible to the user.
iframe.style.display = "none";
// Add the IFRAME to the page. This will trigger
// a request to GenerateFile now.
document.body.appendChild(iframe);
$get('updaetpanel id').className = "progress";
}
function EndRequest()
{
$get('updaetpanel id').className = "normal";
}
By doing this it will redirect to GenerateFile.aspx page and on its page load it will execute the file downloading code, that i posted at top(start of page).

Open div where you click with mouse

If you want to open your div with absolute position near or close to your mouse click. Then use following code:
Syntax(javascript):
//get the scroll postion to use as our offset for our absolute position
var scroll_pos = document.body.scrollTop;
if (scroll_pos == 0) {
if (window.pageYOffset)
scroll_pos = window.pageYOffset;
else
scroll_pos = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
}

now scroll_pos contains top margin height with respect to your mouse. As in eg i set scroll_pos to div:
addDiv.style.marginTop = 100 + parseInt(scroll_pos) + 'px';
Here "addDiv" is clientid of Div.

In javascript find control id in composite databound control

To find the exact id of control that is placed in itemtemplate of composite databound control like (datalist or gridview).Suppose we want control's id onClient click, Call a javascipt function onclientClick and pass a parameter named "event". it is default keyword.
Syntax:

OnClientClick="AddOccupant(event);return false;"
Now go in your javascript function. as in example we go in AddOccupant function in javascript.
Syntax(javascript):

function AddOccupant(event) {
var obj = event.srcElement event.target;
var seltreeNode = obj;
var selectedId = seltreeNode.id;
//now seletedID contains your client side of control that is in compositedatabound control.
}

Javascript function with parameters call onclientclick

To call a javascript function and pass parameters on client click of any button , link button etc. use following syntax:

OnClientClick='<%
"ShowBookingDialog(\""+Eval("CreditCard")+"\",\""+Eval("NonSmoking")+"\",\""+Eval("CreditCardType")+"\",\""+Eval("CreditCardExpiration")+"\",\""+Eval("SrNo")+"\")"
%>
Here my button or link button is in datalist or gridview and i set its datsource.
And "ShowBookingDialog" is a javscript function and passing parameters to it onClientClick.

Thursday, July 23, 2009

Register client script when using Update panel

Page.RegisterClientScriptBlock(string key,string script) will not work if control is inside the update panel. To solve this problem we need to use
ScriptManager.RegisterClientScriptBlock(Control,Type,string key,string script,bool addScriptTags);
Let us take an example suppose a Datalist named "dtalistNames" is in updatepanel and we need to register the script in Datalist's item command event. Then use following text to alert a message.
C# syntax.
string Message="";
Message="";
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "mess", Message, false);