在按钮单击上搜索gridview ItemTemplate文本框值

时间:2011-11-02 14:54:27

标签: asp.net

我的场景是我有一个gridview,我已经采用了itemtemplate文本框和一个按钮,当加载gridview时,用户根据ServiceCode输入一个ServiceCode,点击按钮,数据应该显示在gridview中,当数据在gridview中显示用户输入数量和折扣然后结果应该显示在netamount中,我已经编写了代码但它不起作用我会很感激,如果有人与我分享他们的知识。

我的Axpx代码是:

        <asp:GridView ID="GridView1" runat="server" Height="157px" Width="639px"
        AutoGenerateColumns="False" BorderWidth="1px"
        HorizontalAlign="Justify" onrowdatabound="GridView1_RowDataBound"
        BackColor="LightGoldenrodYellow" BorderColor="Tan" CellPadding="2"
        ForeColor="Black" GridLines="None" onrowcommand="GridView1_RowCommand">
        <FooterStyle BackColor="Tan" />
        <PagerStyle BackColor="PaleGoldenrod" ForeColor="DarkSlateBlue"
        HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" />
        <HeaderStyle BackColor="#FFFFC4" Font-Bold="True" ForeColor="#BF6000"/>
        <Columns>
        <asp:TemplateField HeaderText="S.No">
        <ItemTemplate>
        <asp:Label ID="label10" runat="server" Font-Bold="true" ForeColor="#BF6000"        Text="<%# Container.DataItemIndex + 1 %>">
        </asp:Label>
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Serv Code" >
        <ItemTemplate>
        <asp:TextBox ID="TxtServiceCode" Font-Bold="true" ForeColor="#BF6000" Text='<%#  DataBinder.Eval(Container,"DataItem.ServiceCode") %>'
         runat="server" Width="55px">
        </asp:TextBox>

        <asp:Button ID="Insert" runat="server" BackColor="Ivory" CommandName="InsertRecord"
        Font-Bold="True" ForeColor="#BF6000" Text="Insert" onclick="Insert_Click"/>

         </ItemTemplate>
           </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
       <ItemTemplate>
          <asp:TextBox ID="TxtName" runat="server" ReadOnly="true" Font-Bold="true"           ForeColor="#BF6000" Width="150px" Text='<%#  DataBinder.Eval(Container, "DataItem.ServiceName") %>'></asp:TextBox>
          </ItemTemplate>
         </asp:TemplateField>
          <asp:TemplateField HeaderText="Serv Amt">
        <ItemTemplate>
        <asp:TextBox ID="TxtServiceAmount" ReadOnly="true" Font-Bold="true" ForeColor="#BF6000" Text='<%# DataBinder.Eval(Container,"DataItem.ServiceAmount") %>'
           runat="server" Width="55px"></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
         <asp:TemplateField HeaderText="Qty">
        <ItemTemplate>
          <asp:TextBox ID="TxtQuantity" Font-Bold="true" ForeColor="#BF6000" Text='<%#       DataBinder.Eval(Container,"DataItem.Quantity") %>'
         runat="server" Width="55px"></asp:TextBox>
           </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Disc Amt">
        <ItemTemplate>
          <asp:TextBox ID="TxtdiscAmt" Font-Bold="true" ForeColor="#BF6000" Text ='<%# DataBinder.Eval(Container,"DataItem.Discount") %>'
AutoPostBack="true" OnTextChanged="TxtdiscAmt_TextChanged" runat="server" Width="55px">                </asp:TextBox>
             </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Net Amt">
            <ItemTemplate>
               <asp:TextBox ID="lblNet" runat="Server" Font-Bold="true"  ForeColor="#BF6000" Width="55" />
            </ItemTemplate>
          </asp:TemplateField>
         </Columns>
           <AlternatingRowStyle BackColor="PaleGoldenrod" />
           </asp:GridView>

我的CS代码是:

 DataSet dss = new DataSet();
  SqlConnection MyConnection = new SqlConnection("server=prog;database=mydatabase; UID=sa;PWD=nato123;");

   SqlCommand sqlcmd = new SqlCommand("select * from [Bill]", MyConnection);

   SqlDataAdapter adp = new SqlDataAdapter(sqlcmd);
   DataSet ds = new DataSet();
    adp.Fill(ds);

   GridView1.DataSource = ds.Tables[0];

   GridView1.DataBind();
    } 

1 个答案:

答案 0 :(得分:2)

我会使用OnClick而不是使用按钮OnCommand来传递CommandArgument中的项目索引:

<asp:Button ID="Button1" runat="server" OnCommand="Button1_Command" CommandArgument='<%#Container.ItemIndex%>' />

代码隐藏:

protected void Button1_Command(object sender, CommandEventArgs e)
{
    GridViewRow row = GridView1.Rows[(int)e.CommandArgument];
    if (row != null)
    {
        TextBox txt = row.FindControl("TextBox1") as TextBox;
        if (txt != null)
        {
            string value = txt.Text;
        }
    }
}