【问题标题】:How to populate multiple textboxes from a row in gridview that uses a link button如何从使用链接按钮的gridview中的一行填充多个文本框
【发布时间】:2013-10-25 21:17:35
【问题描述】:

当我单击链接按钮(实际上是每一行中的一个字段的名称)时,我试图用来自 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>

【问题讨论】:

  • 请解释一下——“没有通过”是什么意思。

标签: c# asp.net sql gridview


【解决方案1】:

我让它以这种方式工作 - 使用此站点的帮助:

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

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

    ......
}

我不知道这个声明是否正确,但它确实有效。但是现在它可以工作了,我发现了一个我以前没有看到的问题 - 请查看我的个人资料并非常感谢!

【讨论】:

    【解决方案2】:

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

    标记:

    <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          
        }
    }
    

    【讨论】:

    • 您好,非常感谢您的即时回复。我使用网站上的其他代码让它工作。但是现在更新面板本身正在显示两个文本框。一个点击了值,一个为空。请看那个新问题。非常感谢!
    • @New2This - 很高兴我能提供帮助,也请随时为这个答案投票。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多