0

我正在使用模板字段创建一个网格视图。我在网格视图中使用模板字段插入了一个链接按钮。GridView 之外有 4 个文本框。我想在单击链接按钮时选择行并将所选行的数据放在文本框中。我什至为此使用了行命令,但它不起作用......我使用的语法是.:

<asp:GridView ID="gview" AutoGenerateColumns="False" runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="None" onrowcommand="gview_RowCommand">
    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Book Name">
<ItemTemplate>
<%#Eval("book_name") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Author">
<ItemTemplate>
<%#Eval("book_author") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Publisher">
<ItemTemplate>
<%#Eval("book_Publisher") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Book Price">
<ItemTemplate>
<%#Eval("book_Price") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<ItemTemplate>


<asp:LinkButton ID="lnkDet" CommandName="cmdBind" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"  runat="server" CausesValidation="false">View Details</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>   

</Columns>

和文件后面的代码:

protected void gview_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "cmdBind")
    {
        LinkButton lb = (LinkButton)e.CommandSource;
        int index = Convert.ToInt32(lb.CommandArgument);


        //Bind values in the text box of the pop up control
        txt_name.Text = gview.Rows[index].Cells[0].Text;
        txt_author.Text = gview.Rows[index].Cells[1].Text;
        txt_price.Text = gview.Rows[index].Cells[2].Text;
    }
}

谁能告诉我如何做到这一点。

4

2 回答 2

3

我认为这有效:

protected void gview_RowCommand(object sender, GridViewCommandEventArgs e){
if (e.CommandName == "cmdBind")
{

    int index = Convert.ToInt32(e.CommandArgument);


    //Bind values in the text box of the pop up control
    txt_name.Text = gview.Rows[index].Cells[0].Text;
    txt_author.Text = gview.Rows[index].Cells[1].Text;
    txt_price.Text = gview.Rows[index].Cells[2].Text;
}}
于 2012-11-09T11:18:41.410 回答
0

以您的方式尝试它并非不可能,但只为每一行使用内置的选择按钮可能会更容易。这样你就可以使用 gridview 的 SelectedIndexChanged 事件:

protected void gview_SelectedIndexChanged(object sender, EventArgs e)
{
    txt_name.Text = gview.SelectedRow.Cells[0].Text;
    txt_author.Text = gview.SelectedRow.Cells[1].Text;
    txt_price.Text = gview.SelectedRow.Cells[2].Text;
}
于 2013-11-18T10:45:16.420 回答