|
-
Jul 25th, 2005, 05:24 AM
#1
Thread Starter
Lively Member
Checkbox within Datagrid
Hi All,
I have a datarid and within thi s Datagrid i have a Checkbox and a text box.
what i want is as soon as i click on any checkbox correspondin text box should be populated with some value say 1.
I dont know how to do this.
Some one gave me idea to do this with Javascript but i am totally lost.
Can any one help.
I hope my problem is clear to you all.
Please tell ASAP
thanks iun advance.
-Sachien
Last edited by sachien; Aug 1st, 2005 at 03:42 AM.
Reason: Resolved
-
Jul 25th, 2005, 10:57 AM
#2
Frenzied Member
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
If I have been helpful please rate my post. If I haven't tell me!
-
Jul 25th, 2005, 09:18 PM
#3
Thread Starter
Lively Member
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
-
Jul 26th, 2005, 03:48 AM
#4
Frenzied Member
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
If I have been helpful please rate my post. If I haven't tell me!
-
Jul 27th, 2005, 02:03 AM
#5
Thread Starter
Lively Member
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
-
Jul 27th, 2005, 03:43 AM
#6
-
Jul 28th, 2005, 02:36 AM
#7
Thread Starter
Lively Member
Re: Checkbox within Datagrid [RESOLVED]
Thanks Buddy
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|