1

当我单击链接按钮(实际上是每行中一个字段的名称)时,我试图用来自 gridview 的数据填充多个文本框,但它没有通过。我对此很陌生-实际上是我的第一次。非常感激任何的帮助。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Select")
    {
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = GridView1.Rows[index];

        AccountNumber.Text = selectedRow.Cells[1].Text;
        Name.Text = selectedRow.Cells[1].Text;
        Address1.Text = selectedRow.Cells[1].Text;
        Address2.Text = selectedRow.Cells[2].Text;
        Address3.Text = selectedRow.Cells[3].Text;
        PhoneNumber.Text = selectedRow.Cells[4].Text;
        FaxNumber.Text= selectedRow.Cells[5].Text;
        CurrencyID.Text = selectedRow.Cells[6].Text;
    }
}

<asp:GridView ID="GridView1" runat="server" 
              AutoGenerateColumns="False" 
              DataKeyNames="Agent_Account_No" 
              DataSourceID="SqlDataSource1" 
              AlternatingRowStyle-BackColor ="Lavender" 
              HeaderStyle-BackColor="#9966FF" 
              AllowSorting="True" HeaderStyle-BorderColor="Black"    
              HorizontalAlign="Center" 
              RowStyle-BorderColor="Black" 
              EmptyDataText="There are no data records to display."  
              onrowcommand ="GridView1_RowCommand">
    <AlternatingRowStyle BackColor="#CCFFCC" />
    <Columns>
        <asp:BoundField datafield="Agent_Account_No" HeaderText="Account No" 
                        ItemStyle-HorizontalAlign="Center" 
                        ItemStyle-VerticalAlign="Middle" 
                        ItemStyle-Width="50" 
                        SortExpression="Agent_Account_No" 
                        ReadOnly="true"> 
            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" 
                       Width="50px" />
        </asp:BoundField>
        <asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
            <ItemTemplate> 
                <asp:LinkButton ID="AgentName" runat="server" 
                                Text='<%# Eval("Agent_Name") %>' 
                                CommandName="Select" 
                                CommandArgument='<%#Bind("Agent_Name") %>'> 
                </asp:LinkButton> 
            </ItemTemplate>
        </asp:TemplateField>
4

2 回答 2

1

I got it to work this way - using help from this site:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;        

    AccountNumber.Text = GridView1.DataKeys[row.RowIndex]["Agent_Account_No"].ToString();

    ......
}

I don't know if this declaration is right, but it works. However now that it works I see a problem that I didn't see before - see my profile and thanks a lot!

于 2013-10-30T21:09:20.923 回答
0

我强烈建议TemplateField您对所有列使用 s,如下所示:

标记:

<Columns>
    <asp:TemplateField HeaderText="Account No" SortExpression="Agent_Account_No">
        <ItemTemplate> 
            <asp:Label ID="LabelAccountNumber" runat="server" 
                       Text='<%# Eval("Agent_Account_No") %>'>
            </asp:Label> 
        </ItemTemplate>
    </asp:TemplateField>
    ...
    <asp:TemplateField HeaderText="Name" SortExpression="Agent_Name">
        <ItemTemplate> 
            <asp:LinkButton ID="AgentName" runat="server" 
                            Text='<%# Eval("Agent_Name") %>' 
                            CommandName="Select" 
                            CommandArgument='<%#Bind("Agent_Name") %>'>
            </asp:LinkButton> 
        </ItemTemplate>
    </asp:TemplateField>
</Columns>

现在在RowCommand方法中,您可以使用FindControl()网格视图行的方法来获取您感兴趣的文本,如下所示:

代码隐藏:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Select") 
    { 
        int index = Convert.ToInt32(e.CommandArgument); 
        GridViewRow selectedRow = GridView1.Rows[index];

        // Find the account number label to get the text value for the text box
        Label theAccountNumberLabel = selectedRow.FindControl("LabelAccountNumber") as Label;

        // Make sure we found the label before we try to use it
        if(theAccountNumberLabel != null)
        {
            AccountNumber.Text = theAccountNumberLabel.Text;
        }
        // Follow the same pattern for the other labels to get other text values          
    }
}
于 2013-10-25T21:42:39.767 回答