简体   繁体   English

如何将值存储在gridview asp.net C#中的超链接所选择的行中?

[英]How to store value in hyperlink link selected row in gridview asp.net C#?

I have a gridview.I have a hyperlink field and I want that when I click on hyperlink field, then row value is store in session and page redirect to other page. 我有一个gridview。我有一个超链接字段,我想当我单击超链接字段时将行值存储在会话中并将页面重定向到其他页面。 How can I do this? 我怎样才能做到这一点?

Here is my aspx markup: 这是我的aspx标记:

<asp:GridView ID="GridView1" runat="server" Height="36px" 
    style="margin-left: 270px; margin-top: 92px" Width="232px" CellPadding="3" 
    BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" 
    BorderWidth="1px" onrowcancelingedit="GridView1_RowCancelingEdit" 
    onrowediting="GridView1_RowEditing" onrowupdated="GridView1_RowUpdated" 
    onrowupdating="GridView1_RowUpdating" AutoGenerateColumns="False" 
    onrowdeleting="GridView1_RowDeleting">
    <Columns>

        <asp:TemplateField HeaderText="Table Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtTBL" runat="server" Text='<%# Eval("Table_Name") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Table_Name") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>


        <asp:CommandField HeaderText="Operation" ShowEditButton="True" 
            ShowDeleteButton="True" />

        <asp:TemplateField>
        <ItemTemplate>
           <asp:HyperLink  Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
        </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    <FooterStyle BackColor="White" ForeColor="#000066" />
    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
    <RowStyle ForeColor="#000066" />
    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#007DBB" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>

And here is my code behind : 这是我后面的代码:

public partial class DisplayTable : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         if (!Page.IsPostBack)
         {
             // Retrieve database gridview
             gettable();
         }
    }

    public void gettable()
    {
        // here is code 
    } 

    //here all code included regarding to edit,delete etc
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string s = ((HyperLink)GridView1.SelectedRow.Cells[0].Controls[0]).Text;
        Session["destype"] = s;
        Page.Response.Redirect("home.aspx");
    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        // base.VerifyRenderingInServerForm(control);
    }
}

Hope following will work for you, Not sure as not tested by me. 希望以下内容对您有用,不确定是否未经我的测试。

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gr = ((sender as HyperLink).NamingContainer as GridViewRow);
    Session["destype"] = gr.Cells[0].Text.Trim(); /*For first cell value of Row */
    //Session["abc"] = gr.Cells[2].Text.Trim(); /*Repeat for other cell values of Row by increasing cell index */
    Response.Redirect("~/home.aspx");        
}

Also have look on Send (Pass) GridView Row Values to next page using Session variable in ASP.Net 也可以使用ASP.Net中的Session变量将GridView行值发送(传递)到下一页

I've never been a fan of accessing information by cell reference when it comes from a backing store like a database. 当信息来自像数据库这样的后备存储时,我从来都不喜欢按单元格引用访问信息。

There are a number of ways to accomplish this. 有多种方法可以实现此目的。 I prefer making the information available to the link button. 我更喜欢将信息提供给链接按钮。

In your markup you can do this: 在标记中,您可以执行以下操作:

    <ItemTemplate>
        <asp:LinkButton Text="Select" ID="lnkSelect" runat="server"
                       data-tablename='<%# Eval("Table_Name") %>' 
                       CommandName="Select" 

                       // And consider doing this as often as needed
                       // It should be self evident :)
                       CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
        />
    </ItemTemplate>

or equivalently, in the RowDatabound Event: 或等效地,在RowDatabound事件中:

    protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e ) {
        if ( e.Row.RowType == DataControlRowType.DataRow ) {
            DataRowView drv = (DataRowView) e.Row.DataItem ;
            LinkButton lnkSelect = (LinkButton) e.Row.FindControl("lnkSelect");

            lnkSelect.Attributes["data-tablename"] = drv["Table_Name"];
        }
    }

then in SelectedIndexChanged 然后在SelectedIndexChanged

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView gv = (GridView) sender;
        LinkButton lnkSelect = ( LinkButton ) gv.Rows[ gv.SelectedIndex ].FindControl( "lnkSelect" );

        Session[ "destype" ] = lnkSelect.Attributes[ "data-tablename" ];
        Page.Response.Redirect("home.aspx");
    }

or in RowCommand 或在RowCommand

    protected void GridView1_RowCommand( object sender, GridViewCommandEventArgs e ) {
        if ( e.CommandName == "Select" ) {
            LinkButton lnkSelect = ( LinkButton ) e.CommandSource;
            Session[ "destype" ] = lnkSelect.Attributes[ "data-tablename" ];
            Page.Response.Redirect("home.aspx");
        }
    }

Addendum 附录

NOTE: Hyperlinks are not reliable if you do not provide an href . 注意:如果您不提供href则超链接不可靠。 Since you need to postback first before redirecting to home.aspx, change the HyperLink to a LinkButton . 由于在重定向到home.aspx之前需要先回发,因此请将HyperLink更改为LinkButton I've revised my entire answer accordingly. 我已经相应地修改了我的整个答案。

In the aspx markup, modify the hyperlink with this: 在aspx标记中,使用以下命令修改超链接:

    <ItemTemplate>
        <asp:LinkButton ID="lnkSelect" runat="server"
                        Text="Select" 
                        CommandName="Select" 
                        data-tablename='<%# Eval("Table_Name") %>' />
    </ItemTemplate>

And in SelectedIndexChanged do this 然后在SelectedIndexChanged执行此操作

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridView gv = (GridView) sender;
        LinkButton lnkSelect = ( LinkButton ) gv.Rows[ gv.SelectedIndex ].FindControl( "lnkSelect" );

        Session[ "destype" ] = (string) lnkSelect.Attributes[ "data-tablename" ];
        Page.Response.Redirect("home.aspx");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM