2014-12-13 66 views
0

我已經在網格視圖中使用了3個文本框。我想訪問按鈕單擊事件中單個標籤上的所有3個文本框值。 ASP代碼:無法訪問在gridview控件上的按鈕點擊的文本框值

<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; 
     } 
} 

所以請告訴我,寫代碼了。

+0

刪除的IsPostBack條件 – Dhaval 2014-12-13 06:39:13

+0

ok.let我檢查它是否有效或不.. – 2014-12-13 06:41:36

+0

沒有dhaval..it不起作用!我使它成爲虛假的autopostback。 – 2014-12-13 06:47:31

回答

0

你是「矛盾」你自己的aspxcode behind代碼。爲什麼我這樣說是因爲,你查馬克標籤向上&按鈕單擊事件處理程序: -

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

這裏你Visible屬性設置標籤true這是沒有用的,因爲它默認爲真。現在,看到你的按鈕單擊事件你要把它用lbltxtqty.Visible = true;可見,所以,很顯然你的加價首先應該是這樣的: -

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

因爲,你必須需要網頁加載時。

現在,來你的按鈕點擊事件。你知道ASP.NET的Page Life Cycle嗎?當按鈕點擊事件被觸發,這是一個明顯的回發請求,那麼在那裏檢查IsPostBack屬性的要點是什麼?刪除此代碼,它應該工作。更改按鈕單擊處理程序如下: -

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

我根據您的建議做了更改,但它不起作用。該標籤不顯示任何內容。 lbltxtqty.Text =((TextBox)GridView1.Rows [0] .FindControl(「txtqtys」))。你認爲這段代碼是按照在網格視圖中添加的列寫入的,意思是它的行沒有行[0] – 2014-12-15 05:13:24

+0

@PratikPatani - '行[0]'表示您正在嘗試獲取第一行的文本框值。另外,你在做什麼'TextBox_Changed'事件?如果你沒有做任何動作,請刪除它。 – 2014-12-15 05:56:15

相關問題