Re: Checkbox within Datagrid
Question is do you want to do this client-side (i.e. with JavaScript) or server-side (in which case a postback will have to take place)?
DJ
Re: Checkbox within Datagrid
I would like to do it with client side script.Beacuse if I will use a server side script then every time a check box is clicked form will be posted back and there can be N number of checkbox in the form.
So it will make tedious for the user if form will be Posted again and again.
I hope i made the situation much clear
Re: Checkbox within Datagrid
Ok you need to write a JavaScript function that is called on the client-side onclick event of the CheckBox.
Client-side JavaScript function:
Code:
<script language="javascript" type="text/javascript">
function setTextBoxValue(checkBoxID, textBoxID) {
if (window.document.forms[0].elements[checkBoxID].checked) {
window.document.forms[0].elements[textBoxID].value = "Something";
}
}
</script>
Substitute "Something" with the value you wish to set
At runtime you'll have to attach the client-side onclick event to the checkbox passing the IDs of the CheckBox and TextBox as arguments. You'll have to do this in the ItemDataBound event of the DataGrid:
Code:
void DataGrid1_ItemDataBound( object s, DataGridItemEventArgs e ) {
if ( e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer ) {
CheckBox chkTemp = (CheckBox) e.Item.FindControl("CheckBox1");
TextBox txtTemp = (TextBox) e.Item.FindControl("TextBox1");
chkTemp.Attributes["onclick"] = String.Format("setTextBoxValue('{0}','{1}');", chkTemp.ClientID, txtTemp.ClientID);
}
}
Substitute DataGrid1, CheckBox1, TextBox1 with the ID you have used for the controls
Basically this method is called when each item is data bound to the DataGrid. Firstly it checks if the item being data bound is a header or footer - obviously it ignores these as they won't have any controls in them. It then loads the CheckBox and TextBox for that item into local variables using the rather useful FindControl method (this finds a control just by it's ID). Finally it assigns the onclick attribute to the CheckBox with the JavaScript function created earlier and also the client-side IDs the two controls will take.
Any problems or questions please give a shout.
DJ
Re: Checkbox within Datagrid
Firdt of all thanks DJ for providing that code.
It worked.Thanks Once again.
I will be very kind of u if u please can elaborate that code more for me specially thi s line
HTML Code:
chkTemp.Attributes["onclick"] = String.Format("setTextBoxValue('{0}','{1}');", chkTemp.ClientID, txtTemp.ClientID);
Thanks a lot once again
Re: Checkbox within Datagrid
Ok - chkTemp.ClientID returns the client-side ID of the CheckBox control (take a look at the source of the generated HTML and you'll see that controls within controls have different client-side IDs to their server-side IDs), txtTemp.ClientID does the same for the TextBox control.
To set a client-side onclick event you must use the Attributes property as obviously setting chkTemp.OnClick would use the server-side event which isn't what you want. You can use the attributes property to set loads of client-side attributes such as style or class - generally speaking I've only had to use it for events as there doesn't seem to be server-side equivalents for anything else.
Finally the String.Format method is very useful and tidier than just concatenating the strings. It just positions the following arguments according to the index placed in the first argument.
For example: String.Format("{0}{1}{0}{2}", 1, 2, 3) would return "1213".
You can also use formatting in this statement as described:
http://www.stevex.org/cs/blogs/dotte...icles/158.aspx.
For example: String.Format("{0:(###) ###-####}", 18005551212) would return "(800) 555-1212".
HTH
DJ
Re: Checkbox within Datagrid [RESOLVED]