我通常只在数据网格上使用默认的“选择”,但是现在,当用户选择一行时,我需要弹出一个“模式弹出”窗口。因此,我正在尝试使用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的选定值?

最佳答案

尝试这个::

在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();
}

关于c# - GridView SelectedValue通过选择上的LinkBut​​ton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18168887/

10-11 02:03