0

There are two pages named: People.aspx and Profile.aspx.

People.aspx -> contains the name of all users

Profile.aspx -> contains the information of each user

In the first page, People.aspx, I have a Gridview named peopleGridview and when I am loading the page name People.aspx, in Page_Load event I call a method that executes a sql query on database and gets all the fields that I need to put in my Gridview.

The SQL query is like this:

public DataSet PeopleList()
    {
        GetConnection();
        SqlCommand command = new SqlCommand("SELECT Members.ID, Members.Full_Name, Members.Position, Members.Status, Profile.Address" +
                                            " FROM Members" +
                                            " INNER JOIN Profile" +
                                            " ON Members.ID = Profile.ID"+
                                            " ORDER BY Members.Priority, Members.Membership_Date, Members.Full_Name", con);
        SqlDataAdapter da = new SqlDataAdapter(command);//da: DataAdapter 
        DataSet ds = new DataSet();//ds: Dataset
        da.Fill(ds);
        con.Close();
        return ds;
    }

In Gridview I only need to show the fields Full_Name, Position, Status and Address and I do not want to show the value of the field ID (because it is not necessary for ordinary users to know the ID of other users).

I have a button field in my Gridview for each row and I want whenever that I clicked on the button field of the selected row, read the field ID of the selected row (that is not currently shown in Gridview) and put it in the Response.Redirect("Profile.aspx?UserID=" + userid.Text.ToString()); as an argument for the userid in order to send it to Profile.aspx that is another page that contains the information about that user.

How do I do such a work? In addition, how can I get the value of UserID on the side of Profile.aspx

I did the work using Link field in Gridview, however I am not able to do such a work using button field in GridView.

1 Answer 1

3

To get specific columns in grid view first set false value to Auto Generate Column AutoGenerateColumns="False" than bind your required field inside the Gridview Column.

html GridView :

<asp:GridView ID="GridView1" CssClass="gridview" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" >
    <Columns>
        <asp:BoundField DataField="Full_Name"  HeaderText="Full Name" SortExpression="Full Name" />
        <asp:BoundField DataField="Position" HeaderText="Postion" SortExpression="Position" />
        <asp:BoundField DataField="Address"  HeaderText="Address" SortExpression="Address" />
        <asp:TemplateField HeaderText="View Profile">
            <ItemTemplate>
                <div style="text-align: center;">
                    <asp:ImageButton ID="ImgBtnViewInq" runat="server" ImageUrl="~/images/Viewmail.png" 
                        CommandName="ViewProfile" CommandArgument='<%# Eval("UserID") %>' ToolTip="View Profile" />
                </div>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

C# Code behind :

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "ViewProfile")
    {
        // id with get the Userid parameter that we set inside the button CommandArgument='<%# Eval("UserID") %>'
        int id = Convert.ToInt32(e.CommandArgument);

        // Send this value of id to your querystring to redirect to another page
        Response.Redirect("Profile.aspx?UserId=" + id);
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

There is still a problem. I am a little confused. Where is the 'ID' field of my sql query? where did you get the value of 'ID' field of the selected row in your code?
read the comment in my code carefully. it says "CommandArgument='<%# Eval("UserID") %>'" in your grid view button.
the CommandArgument should be 'ID' and not 'UserId' I think. because with your code I had an error and after changing the argument value to 'ID' I have no longer the error. But there is still another problem. when I click the button, the new page does not load and I get the error "Invalid postback or callback argument." What is this for?
dude, you need to change the code as per your need, we can give you logic. We don't know what fields you are using as we don't have a grip of your code. Last of all the things. People in here will help you with logic not Write the code for you.
I do know and I really appreciate your help. I am not an amateur programmer and I understand the logic. I should understand my self that I must change the name of UserId to ID, it is OK. I have another question. I told you when I click the button on the side of the other page I have a problem. It seems that Profile.aspx can not get the value of the UserId in its own side and I get an error like "Invalid postback or callback argument."
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.