[英]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;
}
}
所以請告訴我它的編寫代碼。
您正在“矛盾”自己的aspx
和code 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.