访问 GridView 的 ChildControl 值
我尝试搜索这个问题,出现了很多结果,但不完全是我得到的结果,所以这里是:
我有一个简单的 GridView 控件,我想在提交后访问子控件的值
我正在做这个:
<asp:GridView ID="gvQuery" runat="server" GridLines="None" CellPadding="5" CellSpacing="5"
OnRowDataBound="gvQuery_RowDataBound" ShowHeader="False" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlCondition" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtField1" runat="server" />
<span class="text2">and <asp:TextBox ID="txtField2" runat="server" /></span>
<asp:HiddenField ID="hfFieldName" runat="server" Value='<%# Eval("InternalName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btnSearch" Text=" Search "
onclick="btnSearch_Click" />
然后,在 btnSearch_Click 事件中,我有正常循环
foreach (GridViewRow gvr in gvQuery.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox ch = gvr.FindControl("chkActive") as CheckBox;
DropDownList dd = gvr.FindControl("ddlCondition") as DropDownList;
TextBox t1 = gvr.FindControl("txtField1") as TextBox;
TextBox t2 = gvr.FindControl("txtField2") as TextBox;
HiddenField hf = gvr.FindControl("hfFieldName") as HiddenField;
if (ch.Checked)
{
SearchResultField srf = new SearchResultField();
Field field = fields.Find(x => x.Name == hf.Value);
srf.Name = field.Name;
srf.Operator = dd.SelectedValue;
srf.Owner = field.WhereOwner;
srf.Param1 = t1.Text;
srf.Param2 = t2.Text;
srf.Type = field.FieldType;
sr.Fields.Add(srf);
}
}
}
问题是 CheckBox 总是 Checked = false 即使我检查它!
我需要做什么才能获得帖子值?看来单击后我完全失去了网格中完成的任何操作,我只是得到了空控件。
在我的 aspx 页面直接我有:
<%@ Page
Title=""
Language="C#"
MasterPageFile="~/3Rows.master"
AutoEventWireup="true"
ValidateRequest="false"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>
我确实有这种行为有效的项目,但我无法理解为什么我在这个简单的页面中有这个......
任何人都有线索?
谢谢。
I tried to search for this question and plenty results come up, but not exactly what I'm getting, so here it goes:
I have a simple GridView control and I want to access the value of the child controls once submited
I'm doing this:
<asp:GridView ID="gvQuery" runat="server" GridLines="None" CellPadding="5" CellSpacing="5"
OnRowDataBound="gvQuery_RowDataBound" ShowHeader="False" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField ItemStyle-Width="20px">
<ItemTemplate>
<asp:CheckBox ID="chkActive" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Description" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ddlCondition" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="txtField1" runat="server" />
<span class="text2">and <asp:TextBox ID="txtField2" runat="server" /></span>
<asp:HiddenField ID="hfFieldName" runat="server" Value='<%# Eval("InternalName") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button runat="server" ID="btnSearch" Text=" Search "
onclick="btnSearch_Click" />
and then, in the btnSearch_Click event I have the normal loop
foreach (GridViewRow gvr in gvQuery.Rows)
{
if (gvr.RowType == DataControlRowType.DataRow)
{
CheckBox ch = gvr.FindControl("chkActive") as CheckBox;
DropDownList dd = gvr.FindControl("ddlCondition") as DropDownList;
TextBox t1 = gvr.FindControl("txtField1") as TextBox;
TextBox t2 = gvr.FindControl("txtField2") as TextBox;
HiddenField hf = gvr.FindControl("hfFieldName") as HiddenField;
if (ch.Checked)
{
SearchResultField srf = new SearchResultField();
Field field = fields.Find(x => x.Name == hf.Value);
srf.Name = field.Name;
srf.Operator = dd.SelectedValue;
srf.Owner = field.WhereOwner;
srf.Param1 = t1.Text;
srf.Param2 = t2.Text;
srf.Type = field.FieldType;
sr.Fields.Add(srf);
}
}
}
Problem is that the CheckBox is always Checked = false even if I check it!
What do I need to do, to get the post values? it seams that after clicking I completely loose anything done in the grid, I'm just getting empty controls.
in my aspx page direct I have:
<%@ Page
Title=""
Language="C#"
MasterPageFile="~/3Rows.master"
AutoEventWireup="true"
ValidateRequest="false"
CodeFile="Default.aspx.cs"
Inherits="_Default" %>
I do have projects with this behavior working but I can't seam to understand why do I have this one here in this simple page....
Anyone have a clue?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
孩子的错误......
相反
child mistake ...
instead