按钮点击后如何从gridview中的templatefield获取值?

时间:2014-12-17 21:54:09

标签: c# asp.net gridview

我遵循gridview,我需要从templatefield" VyskaStavky"中的文本框中获取价值。点击按钮后。请有人能告诉我怎么做到这一点? 我试过这样的事            CommandArgument ='<%#Eval(" Id")+"," + Eval(" VyskaStavky")%>' 但我有问题,因为VyskaStavky不是类Kurz的属性(我的datasourceID的命名是来自数据的类+ D的名称和来自源的S)

    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataSourceID="KurzDS" DataKeyNames="Id" OnRowCommand="GridView1_RowCommand">
        <Columns>
            <asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ReadOnly="True" Visible="False" />
            <asp:BoundField DataField="Zapas" HeaderText="Zapas" SortExpression="Zapas" ReadOnly="True" Visible="False" />
            <asp:BoundField DataField="AktKurz" HeaderText="AktKurz" SortExpression="AktKurz" />
            <asp:BoundField DataField="Tip" HeaderText="Tip" SortExpression="Tip" />
            <asp:BoundField DataField="Handicap" HeaderText="Handicap" SortExpression="Handicap" />
    <asp:TemplateField>
        <ItemTemplate>
            <asp:TextBox ID="VyskaStavky" runat="server" Text=""></asp:TextBox>
        </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="Button1" Text='Podat stavku' CommandArgument='<%# Eval("Id")%>'  CommandName="PodatStavku" runat="server" CausesValidation="false" />
            </ItemTemplate>
    </asp:TemplateField>
        </Columns>
    </asp:GridView>

Thanx很多

1 个答案:

答案 0 :(得分:1)

我解决了我的问题。我更改模板按钮(从命令到onclick)

            <ItemTemplate>
                <asp:Button ID="Button1" Text='Podat stavku' OnClick="Button1_Click" runat="server" CausesValidation="false" />
            </ItemTemplate>

并且在c#中我得到了值

    protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = sender as Button;
        GridViewRow row = btn.NamingContainer as GridViewRow;
        string pk = GridView2.DataKeys[row.RowIndex].Values["Id"].ToString();
        TextBox ss = GridView2.Rows[row.RowIndex].Cells[3].FindControl("VyskaStavky") as TextBox;
        //ss.text have my requested value
    }