简体   繁体   中英

transfer data from gridview to another webform in asp.net using c#?

i have a gridview like this :

 <asp:GridView ID="wbsdataGV1" runat="server" Width="790px" AutoGenerateColumns="False"
                                    OnSelectedIndexChanged="wbsdataGV1_SelectionChanged">
                                    <Columns>

                                        <asp:TemplateField HeaderText="WBS Code">
                                            <ItemTemplate>
                                                <asp:LinkButton ID="LinkButton1" runat="server" Text='<%# Eval("WBSCode") %>'></asp:LinkButton>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Description">
                                            <ItemTemplate>
                                                <asp:TextBox ID="TextBox7" runat="server" Text='<%# Eval("Description") %>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Territory Code">
                                            <ItemTemplate>
                                                <asp:TextBox ID="TextBox8" runat="server" Text='<%# Eval("TerritoryCode") %>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                        <asp:TemplateField HeaderText="Amount">
                                            <ItemTemplate>
                                                <asp:TextBox ID="TextBox9" runat="server" Text='<%# Eval("AmountReleased") %>'></asp:TextBox>
                                            </ItemTemplate>
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>

when i am clicking on the linkbutton i want that data and i will transfer that to another aspx from .. through

protected void wbsdataGV1_SelectionChanged(object sender, EventArgs e)
{
    //Some code ;
}

now my question is how i can get that data by clicking the linkbutton on gridview ? i can transfer the data by querystring or session variable or hidden text field ... but my concern is that how can i get the exact clicked data... any help?

Define the DataKeys attribute to the be primary key of your data (here I assume you have a row identifier called "ID".

<asp:GridView ... DataKeys="ID">
    ... existing markup
</asp:GridView>

In wbsdataGV1_SelectionChanged , get back the ID of the row the user clicked like this:

int rowID = (int)this.wbsdataGV1.SelectedDataKey.Value;

Then redirect to another page like this, passing just the ID:

Response.Redirect("anotherpage.aspx?id=" + rowID);

Then in your other page:

protected void Page_Load(object s, EventArgs e)
{
    if (!Page.IsPostback)
    {
        int rowID = int.Parse(Request.QueryString["id"]);

        // do something with the ID of the row, like go and look
        // up JUST that row again
    }
}

Bonus - to make the whole row clickable, avoiding the need for a CommandButton, do this:

<script language="javascript" type="text/javascript">
    var oldColour = null;

    function rowMouseover(o) {
        o.style.cursor = 'pointer';
        oldColour = o.style.backgroundColor;
        o.style.backgroundColor = '#dddddd';
    }

    function rowMouseout(o) {
        o.style.backgroundColor = oldColour;
    }
</script>

<asp:GridView ... OnRowCreated="grid_RowCreated" />

protected void grid_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        e.Row.Attributes["onclick"] =
            Page.ClientScript.GetPostBackClientHyperlink(
                this.grid, 
                "Select$" + e.Row.RowIndex);

        e.Row.Attributes["onmouseover"] = "rowMouseover(this);";
        e.Row.Attributes["onmouseout"] = "rowMouseout(this);";
    }
}

protected override void Render(HtmlTextWriter writer)
{
    for (int i = 0; i < grid.Rows.Count; i++)
        Page.ClientScript.RegisterForEventValidation(grid.UniqueID, "Select$" + i); 

    base.Render(writer);
}

You could pass the ID via the Session object, but you'd get odd behaviour with more than one browser window doing the same operation at the same time (depending on the browser). This would be hard for a user to tamper with.

You could pass the ID via Request.Form too, that would hide it from trivial user fiddling but doesn't add any true security.

Protip: don't just dump massive objects into the Session variable. It's lazy, error prone, and doesn't scale.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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