Results 1 to 2 of 2

Thread: DataGrid template coloumn

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2003
    Location
    Bangalore
    Posts
    47

    DataGrid template coloumn

    Hi
    i am using vb.net code behind for web app.I have 1 dtagrid,in that i have 2 template coloumns.one is check box and another one is textbox.My question is whenever i check the checkbox,related textbox should enable.If i uncheck textbox should be disable.can any body give me piece of code for this 1.



    thanks
    Satish

  2. #2
    Member Nikhil Aggarwal's Avatar
    Join Date
    Jun 2005
    Location
    New Delhi, India
    Posts
    36

    Re: DataGrid template coloumn

    The following solution is to do the activation/deactivation on the client side using javascripts
    1) Insert the following script on the aspx page
    Code:
    <script language="javascript">
    function ToggleTextBox(textboxname,checkboxname){
    
    var txtDisable = document.getElementById(textboxname); var chkDisabled = document.getElementById(checkboxname); if (chkDisabled.checked == true)
    txtDisable.disabled=false;
    else
    txtDisable.disabled=true;
    } </script>
    2) Write the following code in the ItemDataBound Event of the datagrid
    VB Code:
    1. If e.Item.ItemIndex > -1 Then
    2.             Dim chkDisable As CheckBox
    3.             Dim txtDisable As TextBox
    4.             chkDisable = CType(e.Item.FindControl("chkDisable"), CheckBox)
    5.             txtDisable = CType(e.Item.FindControl("txtDisable"), TextBox)
    6.             chkDisable.Attributes.Add("onclick", "javascript:ToggleTextBox('" + txtDisable.ClientID.ToString() + "','" + chkDisable.ClientID.ToString() + "');")
    7.         End If
    3) Your template columns should be as follows
    Code:
    <asp:TemplateColumn>
    
    <ItemTemplate>
    <asp:CheckBox id="chkDisable" runat="server"></asp:CheckBox>
    </ItemTemplate>
    </asp:TemplateColumn> <asp:TemplateColumn>
    <ItemTemplate>
    <asp:TextBox id="txtDisable" runat="server" Enabled="False"></asp:TextBox>
    </ItemTemplate>
    </asp:TemplateColumn>
    What this does is that the text boxes are disabled by default. When the user clicks on the checkbox of a row, it enables or disables the textbox of the same row according to the checked state of the checkbox.
    Hope it solved your problem.
    Nikhil Aggarwal

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