访问 GridView 的 ChildControl 值

发布于 2024-08-06 15:58:52 字数 2886 浏览 6 评论 0原文

我尝试搜索这个问题,出现了很多结果,但不完全是我得到的结果,所以这里是:

我有一个简单的 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&nbsp;<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2024-08-13 15:58:52

孩子的错误......

protected void Page_Load(object sender, EventArgs e)
{
    PopulateData();
}

相反

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        PopulateData();
}

child mistake ...

protected void Page_Load(object sender, EventArgs e)
{
    PopulateData();
}

instead

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        PopulateData();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文