MAC
MAC

Reputation: 6577

gridview editing

In my web page i used a gridview. In this gridview it shows a group of users information. I just added one button from smart tag menu. And my requirement is that when i am hitting the button corresponding to each users, it will redirect to another page and shows the corresponding user's information. What i do for getting this type of output?

Upvotes: 2

Views: 1298

Answers (3)

Austin
Austin

Reputation: 4758

Ahmy's answer is the way to go if you want to use a button and have your page redirect to another page with the user's information. One thing that was left out however was that you can pass a command argument through the button (like the user's unique ID) which you could then put in the querystring of the page you are redirecting to, to identify which user it is. That would look like this:

<asp:TemplateField HeaderText="Edit User">
    <ItemTemplate>
        <asp:Button ID="EditBtn" Text="Edit User" CommandName="Edit" 
            CommandArgument='<%# Eval("UserID") %>' runat="server" /> 
    </ItemTemplate>
</asp:TemplateField>

Then in the code behind

protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        //Redirect to user page information
        Response.Redirect("UserProfilePage.aspx?userID=" + e.CommandArgument);
    }
}

Another alternative to using a button, which I think is the best option is to use a HyperLinkField. When using a button, the page will have to post back to the server, then send a redirect to the user's browser. With a hyperlink, the user goes straight to the correct page. It saves a step and doesn't rely on javascript.

<asp:HyperLinkField DataTextField="UserName" DataNavigateUrlFields="UserID"
    DataNavigateUrlFormatString="UserProfilePage.aspx?userID={0}" 
    HeaderText="Edit User" />

Upvotes: 3

Kaz
Kaz

Reputation: 179

Instead of button, make one of the column hyperlinked. On clicking the item, redirect to your new page (using Javascript). By this you can avoid an additional column for the button and a postback.

You must use DataTextFormatString for this.

Sample

        <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
        <Columns>
            <asp:BoundField DataField="no" HeaderText="SNo" />
            <asp:BoundField DataField="file" DataFormatString="&lt;a href=javascript:ShowAssetDetail('{0}');&gt;{0}&lt;/a&gt;"
                HeaderText="Asset Desc" HtmlEncodeFormatString="False" />
        </Columns>
    </asp:GridView>

In above sample the JS function ShowAssetDetail() must take a value to pass to the redirecting page. Needless to say, the JS function must be written in addition.

Upvotes: 1

Ahmy
Ahmy

Reputation: 5480

U have to add the button and add an attribute CommandName:

<asp:Button ID="EditBtn" runat="server" CommandName="Edit" />

then in the event of itemcommand of the grid do the following

  protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Edit")
        {
            //Redirect to user page information
            Response.Redirect(PageURL);
        }
    }

Upvotes: 3

Related Questions