Results 1 to 7 of 7

Thread: Checkbox within Datagrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    121

    Resolved 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

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    121

    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

  4. #4
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    121

    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

  6. #6
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    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

    If I have been helpful please rate my post. If I haven't tell me!

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    121

    Resolved 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
  •  



Click Here to Expand Forum to Full Width