在TextBox中显示GridView选定的行数据

时间:2014-09-17 18:58:38

标签: c# asp.net gridview

我在这里有一个显示图书详情的GridView

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" BackColor="White"
                BorderColor="#CCCCCC" BorderWidth="2px" CellPadding="2" CellSpacing="5" ForeColor="#000066"
                GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                <RowStyle BackColor="#F7F7F7" />
                <AlternatingRowStyle BackColor="#E7E7FF" />
                <FooterStyle BackColor="White" />
                <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="White" ForeColor="#000066"
                    HorizontalAlign="Center" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#007DBB" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#00547E" />
                <Columns>

                    <asp:CommandField ShowSelectButton="true" ControlStyle-ForeColor="Red" SelectText="Select" HeaderText="Select" />

                    <asp:TemplateField HeaderText="Id">
                        <ItemTemplate>
                            <asp:Label ID="LabelId" runat="server" Text='<%# Bind("Id") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                    <asp:TemplateField HeaderText="Title">
                        <ItemTemplate>
                            <asp:Label ID="LabelTitle" runat="server" Text='<%# Bind("Title") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>

                 </Columns>
</asp:GridView>

现在,如果我想编辑图书详细信息,还有文本框,因此当我点击SelectShowSelectButton="true")时,应在文本框中填充列值。

我在GridView1_SelectedIndexChanged事件中尝试过这样的做法,但点击选择时没有任何反应:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    textBoxID.Text = GridView1.SelectedRow.Cells[0].Text;
    textBoxTitle.Text = GridView1.SelectedRow.Cells[1].Text;
}

4 个答案:

答案 0 :(得分:1)

您需要投射该行的Label

void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
{    
    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = gridview1.SelectedRow;

    //now get the labels
    Label _LabelId = row.FindControl("LabelId") as Label;
    Label _LabelTitle = row.FindControl("LabelTitle") as Label;

    //get the values from labels and assign them to textboxes
    textBoxID.Text = _LabelId.Text;
    textBoxTitle.Text = _LabelTitle.Text;    
}

答案 1 :(得分:1)

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
     TextBoxUserID.Text = GridView1.SelectedRow.Cells[1].Text;
     TextBoxUserName.Text = GridView1.SelectedRow.Cells[2].Text;
}

答案 2 :(得分:0)

您好,您无法获取该值,因为您正在使用ItemTemplateField将其更改为BoundField。你可以在GridView中使用DateKeyNames和Event SelectedIndexChanged

答案 3 :(得分:0)

GridView1_SelectedIndexChanged事件中

GridViewRow gvRow = GridView1.SelectedRow;      
if(gvRow != null)
           {
              Label lblId = gvRow.FindControl("LabelId") as Label;
              Label lblTitle = gvRow.FindControl("LabelTitle") as Label;
              if(lblId != null && lblTitle != null)
              {
                 textBoxID.Text = lblId.Text;
                  textBoxTitle.Text = lblTitle.Text;
               }
            }