2

我通常只在我的数据网格上使用默认的“选择”,但现在,当用户选择一行时,我需要弹出一个模态弹出窗口。所以,我正在尝试使用 ImageButton:

                <Columns>
                    <asp:TemplateField HeaderText="Edit">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtn" ImageUrl="../Images/edit.png" runat="server" Width="25" Height="25" OnClick="edit_Click" CommandName="Select" />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Name" HeaderText="Project Name" />
                    <asp:BoundField DataField="ProjectCode" HeaderText="Code" />
                    <asp:TemplateField HeaderText="Access"></asp:TemplateField>

                </Columns>

我创建了触发的 OnClick 事件,并且我认为我可以在该事件中获取行的 selectedvalue。我通常使用 GridView1_SelectedIndexChanged 事件。

但我正在努力寻找获得 SelectedValue 的方法。

protected void edit_Click(object sender, ImageClickEventArgs e)
{
    ImageButton btndetails = sender as ImageButton;
    GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
    Session["SelectedUserProjectId"] = ???? // This is where I am trying to get the selected value - but can't find a way to do so.
    var pi = int.Parse(Session["SelectedUserProjectId"].ToString());
    var ui = int.Parse(Session["SelectedUserId"].ToString());
    // Do some calls to get data, populate fields, and then...

    this.pnlEdit_ModalPopupExtender.Show();
}

谁能指导我如何获取 GridView 的选定值?

4

2 回答 2

2

你的问题:Can anyone guide me on how to get the selected value of the GridView?

您还需要将CommandArgumentImageButton 的属性设置为所需的 Column 值。说::ProjectId[即ProjectId应该是表中的一列]

<asp:ImageButton ID="imgbtn" ImageUrl="../Images/edit.png" runat="server" 
 Width="25" Height="25" OnClick="edit_Click" CommandName="Select" 
 CommandArgument='<%# Eval("ProjectId") %>' />

在您的edit_Click事件中,您可以检索为:

protected void edit_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton btndetails = sender as ImageButton;
        Session["SelectedUserProjectId"] = btndetails.CommandArgument;
       // Rest of implementation
    }
于 2013-08-11T09:49:24.560 回答
0

试试这个::

在 aspx 中将您的 Id 绑定在隐藏字段中,然后在后面的代码中找到该隐藏字段并获取值

  <Columns>
                    <asp:TemplateField HeaderText="Edit">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbtn" ImageUrl="../Images/edit.png" runat="server" Width="25" Height="25" OnClick="edit_Click" CommandName="Select" />
<asp:Hiddenfield runat="server" ID="hId" value='<%# Eval("UserProjectId")%>' />
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="Name" HeaderText="Project Name" />
                    <asp:BoundField DataField="ProjectCode" HeaderText="Code" />
                    <asp:TemplateField HeaderText="Access"></asp:TemplateField>

                </Columns>

在后面的代码中

protected void edit_Click(object sender, ImageClickEventArgs e)
{
    ImageButton btndetails = sender as ImageButton;
    GridViewRow gvrow = (GridViewRow)btndetails.NamingContainer;
    Session["SelectedUserProjectId"] = ((HiddenField)gvrow.FindControl("hId")).Value // This is where I am trying to get the selected value - but can't find a way to do so.
    var pi = int.Parse(Session["SelectedUserProjectId"].ToString());
    var ui = int.Parse(Session["SelectedUserId"].ToString());
    // Do some calls to get data, populate fields, and then...

    this.pnlEdit_ModalPopupExtender.Show();
}
于 2013-08-11T05:31:23.423 回答