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
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:
If e.Item.ItemIndex > -1 Then
Dim chkDisable As CheckBox
Dim txtDisable As TextBox
chkDisable = CType(e.Item.FindControl("chkDisable"), CheckBox)
txtDisable = CType(e.Item.FindControl("txtDisable"), TextBox)
chkDisable.Attributes.Add("onclick", "javascript:ToggleTextBox('" + txtDisable.ClientID.ToString() + "','" + chkDisable.ClientID.ToString() + "');")
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. :thumb: