2014-12-13 67 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

相关问题