繁体   English   中英

单击按钮时无法访问gridview控件中的文本框值

[英]can't access Textbox value which is in gridview control on button click

我在网格视图中使用了3个文本框。 我想在按钮单击事件中访问单个标签中的所有3个文本框值。 ASP代码:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
        CssClass="style1" ForeColor="#333333" GridLines="None" Height="342px" Width="548px">
        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        <Columns>
            <asp:BoundField ApplyFormatInEditMode="True" DataField="IName" HeaderText="Item Name" />
            <asp:BoundField DataField="Description" HeaderText="Description" />
            <asp:BoundField DataField="price" HeaderText="Price" />
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="txtqty" runat="server" AutoPostBack="false" Text="Enter Quantity" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:TextBox ID="txtqtys" runat="server" AutoPostBack="false" OnTextChanged="TextBox_Changed"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <HeaderTemplate>
                    <asp:Label ID="chkAll" runat="server" onclick="checkAll(this);" AutoPostBack="true"
                        OnCheckedChanged="CheckBox_CheckChanged" Text="Select" />
                </HeaderTemplate>
                <ItemTemplate>
                    <asp:CheckBox ID="chk" runat="server" onclick="Check_Click(this)" AutoPostBack="True"
                        OnCheckedChanged="CheckBox_CheckChanged" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <EditRowStyle BackColor="#999999" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#E9E7E2" />
        <SortedAscendingHeaderStyle BackColor="#506C8C" />
        <SortedDescendingCellStyle BackColor="#FFFDF8" />
        <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
    </asp:GridView>
<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />

C#代码:

protected void btnsubmit_Click(object sender, EventArgs e)
{
     if (!IsPostBack)
        {
            lbltxtqty.Visible = true;
            lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
        }
}

所以请告诉我它的编写代码。

您正在“矛盾”自己的aspxcode behind代码。 我之所以这么说是因为,请检查标签标记和按钮单击事件处理程序:-

<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="true"></asp:Label>

在这里,您将标签的Visible属性设置为true ,这是没有用的,因为默认情况下为true。 现在,使用lbltxtqty.Visible = true; ,查看您试图使其可见的按钮单击事件lbltxtqty.Visible = true; ,因此很明显,您的标记应首先如下所示:-

<asp:Label ID="lbltxtqty" runat="server" Text="txtqty" Visible="false"></asp:Label>

因为,在Page加载时,您必须需要它。

现在,出现您的按钮单击事件。 您知道ASP.NET的Page Life Cycle吗? 当触发按钮单击事件时,这是一个明显的PostBack请求,那么检查IsPostBack属性的IsPostBack是什么? 删除此代码,它应该可以工作。 如下更改按钮单击处理程序:

protected void btnsubmit_Click(object sender, EventArgs e)
{
    lbltxtqty.Visible = true;
    lbltxtqty.Text = ((TextBox)GridView1.Rows[0].FindControl("txtqtys")).Text;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM