本文介绍了选中“关联”复选框后,启用gridview中存在的texbox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 嗨 我正在开发一个项目,因为我在数据网格中有Datagrid,我有两个名为checkbox和textbox的控件。 我的要求: 我最初需要的是数据网格内的所有文本框都被禁用。当我点击时复选框然后单独关联的文本框应该启用并应该设置焦点,如果我再次取消选中它然后它应该禁用。 我实现的是: 我创建了datagrid,checkbox和textbox。当单击复选框时,其关联的文本框将获得焦点。 我寻求的是: 真的我不知道如何根据复选框点击启用和禁用文本框。 这是我的aspx代码: < blockquote class = FQ > < div class = FQA > 引用:< / div > &l t; asp:DataGrid ID = dgModuleSearch runat = server BorderWidth = 1px BorderColor = #FE9B00 BorderStyle = 实体 BackColor = 白色 AllowPaging = True 字体名称 = Verdana 字体大小 = 中 AutoGenerateColumns = False AllowSorting = True OnPageIndexChanged = dgModuleSearch_PageIndexChanged OnItemCommand = dgModuleSearch_Select PageSize = 25 OnItemDataBound = dgModuleSearch_DataBound > < SelectedItemStyle 字体粗体 = True ForeColor = 黑色 BackColor = 雪 > < / SelectedItemStyle > < EditItemStyle BackColor = AntiqueWhite > < / EditItemStyle > < PagerStyle BackColor = #FDE9CB ForeColor = #003399 Horizo​​ntalAlign = 右 模式 = NumericPages 位置 = 底部 字体大小 = 小 字体粗体 = true / > < AlternatingItemStyle BackColor = Snow > < / AlternatingItemStyle > < ItemStyle ForeColor = #000066 BackColor = Snow > < / ItemStyle > < HeaderStyle 字体大小 = XX-Small 字体粗体 = 真 高度 = 10px ForeColor = #000000 BackColor = #FFDBA6 > < / HeaderStyle > < 列 > < asp:TemplateColumn HeaderText = 选择 > < HeaderStyle 宽度 = 5% 字体下划线 = true / > < ItemTemplate > ID =cb1runat =serverWidth =265pxCommandName =SelectItem OnCheckedChanged =cb1_CheckedChangedText ='<%#DataBinder.Eval(Container.DataItem, 模块)%> /> < / ItemTemplate > < / asp:TemplateColumn > < asp:TemplateColumn HeaderText = 没有工作/交易 > < HeaderStyle 宽度 = 5% 字体下划线 = true / > < ItemTemplate > < asp:TextBox ID = tb1 runat = 服务器 AutoPostBack = true 列 = 5 文本 = 宽度 = 265px 已启用 = true / > < / ItemTemplate > < / asp:TemplateColumn > < asp:BoundColumn DataField = ID HeaderText = ID 可见 = false > < / asp:BoundColumn > < /列 > < / asp:DataGrid > < / blockquote > 这是我的code.cs: if((e.Item.ItemType == ListItemType.Item)|| (e.Item.ItemType == ListItemType.AlternatingItem)) { CheckBox cb1 =(CheckBox)e.Item.FindControl(cb1); TextBox tb1 =(TextBox)e.Item.FindControl(tb1); if(cb1.Checked == true) { tb1.Enabled = true; cb1.Attributes.Add(onclick,javascript:if(this.checked){document.getElementById('+ tb1.ClientID +')。focus()}); } 其他 { tb1.Enabled = false; } } 如果有人帮我解决这个问题,那将是值得赞赏的。 先谢谢。解决方案 你需要添加 AutoPostBack =如果您需要在 cb1_CheckedChanged CheckBox 为真 C $ C>事件;尝试下面 public void cb1_CheckedChanged( object sender,EventArgs e) { CheckBox chk =(CheckBox)sender; GridViewRow objItem =(GridViewRow)chk.Parent.Parent; CheckBox cb1 =(CheckBox)objItem.FindControl( cb1); TextBox tb1 =(TextBox)objItem.FindControl( tb1); tb1.Visible = cb1.Checked; } 嗨这是我如何使我的项目工作: foreach(dgModuleSearch.Items中的DataGridItem dr) { CheckBox cb1 =(CheckBox)dr.FindControl(cb1); TextBox tb1 =(TextBox)dr.FindControl(tb1); // if(dr.Cells [0] .Text!= null&& dr.Cells [0] .Text!=) if(cb1.Checked) { tb1.Visible = cb1.Checked; cb1.Attributes.Add(onclick,javascript:if(this.checked){document.getElementById('+ tb1.ClientID +')。focus()}); } } 我需要代码反之亦然 (即)可以有人说当数据网格中的复选框未选中时,如何将数据网格中文本框的启用设置为false。 先谢谢。 HiI'm developing an project in that i have Datagrid inside the data grid i have 2 controls named checkbox and textbox.My Requirement:What i need is initially all the textbox inside the datagrid is disabled.when i click the check box then its associated textbox alone should get enable and should set focus in that and if i again uncheck it then it should disable.What i acheived is:I created the datagrid , checkbox and textbox. and when the check box is clicked its associated textbox is getting focus.What i seek for is:Really i dont know how to enable and disable the textbox based on check box click.Here is my aspx code:<blockquote class="FQ"><div class="FQA">Quote:</div><asp:DataGrid ID="dgModuleSearch" runat="server" BorderWidth="1px" BorderColor="#FE9B00" BorderStyle="Solid" BackColor="White" AllowPaging="True" Font-Names="Verdana" Font-Size="Medium" AutoGenerateColumns="False" AllowSorting="True"OnPageIndexChanged="dgModuleSearch_PageIndexChanged" OnItemCommand="dgModuleSearch_Select" PageSize="25" OnItemDataBound="dgModuleSearch_DataBound"><SelectedItemStyle Font-Bold="True" ForeColor="Black" BackColor="Snow"></SelectedItemStyle><EditItemStyle BackColor="AntiqueWhite"></EditItemStyle><PagerStyle BackColor="#FDE9CB" ForeColor="#003399" HorizontalAlign="Right" Mode="NumericPages" Position="Bottom" Font-Size="Small" Font-Bold="true" /><AlternatingItemStyle BackColor="Snow"></AlternatingItemStyle><ItemStyle ForeColor="#000066" BackColor="Snow"></ItemStyle><HeaderStyle Font-Size="XX-Small" Font-Bold="True" Height="10px" ForeColor="#000000"BackColor="#FFDBA6"></HeaderStyle><Columns><asp:TemplateColumn HeaderText="Select"><HeaderStyle Width="5%" Font-Underline="true" /><ItemTemplate>ID="cb1" runat="server" Width ="265px" CommandName="SelectItem"OnCheckedChanged="cb1_CheckedChanged" Text='<%# DataBinder.Eval(Container.DataItem, "Modules")%>' /></ItemTemplate></asp:TemplateColumn><asp:TemplateColumn HeaderText="No of Jobs/Transactions"><HeaderStyle Width="5%" Font-Underline="true" /><ItemTemplate><asp:TextBox ID="tb1" runat="server" AutoPostBack="true" Columns="5" Text="" Width ="265px" Enabled="true"/></ItemTemplate></asp:TemplateColumn><asp:BoundColumn DataField="ID" HeaderText="ID" Visible="false"></asp:BoundColumn></Columns></asp:DataGrid></blockquote>Here is my code.cs:if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) { CheckBox cb1 = (CheckBox)e.Item.FindControl("cb1"); TextBox tb1 = (TextBox)e.Item.FindControl("tb1"); if (cb1.Checked == true) { tb1.Enabled = true; cb1.Attributes.Add("onclick", "javascript:if(this.checked){document.getElementById('" + tb1.ClientID + "').focus()}"); } else { tb1.Enabled = false; } }It would be appreciatable if some one helps me to solve this.Thanks in Advance. 解决方案 you need to add AutoPostBack="True" for your CheckBoxif you need to hide textbox in cb1_CheckedChanged event; try below public void cb1_CheckedChanged(object sender, EventArgs e){ CheckBox chk = (CheckBox)sender; GridViewRow objItem = (GridViewRow)chk.Parent.Parent; CheckBox cb1 = (CheckBox)objItem.FindControl("cb1"); TextBox tb1 = (TextBox)objItem.FindControl("tb1"); tb1.Visible = cb1.Checked; }Hi This is How i Make my Project to work:foreach (DataGridItem dr in dgModuleSearch.Items) { CheckBox cb1 = (CheckBox)dr.FindControl("cb1"); TextBox tb1 = (TextBox)dr.FindControl("tb1"); // if (dr.Cells[0].Text != null && dr.Cells[0].Text != "") if (cb1.Checked) { tb1.Visible = cb1.Checked; cb1.Attributes.Add("onclick", "javascript:if(this.checked){document.getElementById('" + tb1.ClientID + "').focus()}"); } }I need the code for vise versa(i.e)Can Some one say how to set the enable of the textbox in the datagrid to false when check box in the datagrid is is unchecked.thanks in Advance. 这篇关于选中“关联”复选框后,启用gridview中存在的texbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-03 01:33