简体   繁体   English

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

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

I have taken 3 text-boxes in grid view. 我在网格视图中使用了3个文本框。 I want to access all the 3 text-boxes value in single label on button click event. 我想在按钮单击事件中访问单个标签中的所有3个文本框值。 ASP code: 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# code: C#代码:

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

so please tell me the write code for it. 所以请告诉我它的编写代码。

You are "Contradicting" your own aspx and code behind code. 您正在“矛盾”自己的aspxcode behind代码。 Why I am saying this is because, check you Label Mark-up & button click event handler:- 我之所以这么说是因为,请检查标签标记和按钮单击事件处理程序:-

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

Here you are setting labels Visible property to true which is of no use because its by default true. 在这里,您将标签的Visible属性设置为true ,这是没有用的,因为默认情况下为true。 Now, see your button click event you are trying to make it visible by using lbltxtqty.Visible = true; 现在,使用lbltxtqty.Visible = true; ,查看您试图使其可见的按钮单击事件lbltxtqty.Visible = true; , So obviously your mark-up should first look like this:- ,因此很明显,您的标记应首先如下所示:-

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

Because, you must be needing that when Page loads. 因为,在Page加载时,您必须需要它。

Now, comes your button click event. 现在,出现您的按钮单击事件。 Do you know the Page Life Cycle of ASP.NET? 您知道ASP.NET的Page Life Cycle吗? When button click event is fired that's an obvious PostBack request so what is the Point of checking IsPostBack property there? 当触发按钮单击事件时,这是一个明显的PostBack请求,那么检查IsPostBack属性的IsPostBack是什么? Remove this code and it should work. 删除此代码,它应该可以工作。 Change your button click handler as follows:- 如下更改按钮单击处理程序:

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