Home > Back-end >  ASP how to call linkbuttonid text to a var?
ASP how to call linkbuttonid text to a var?

Time:11-15

How do i call the linkbutton userid to the var searchtext?

i currently am able to display the list of user profile data and am wondering if its possible to input the linkbuttonid into the var searchtext?

asp.cs

protected void btnview_Click(object sender, EventArgs e)
        {


            var searchText = ""; 
            Response.Redirect("~/viewprofile.aspx?srch="   searchText);
        }

Asp

    <asp:GridView ID="grduserprofile" AutoGenerateColumns="False" GridLines="Horizontal" Width="50%" Style="min-width: 160px"
                CssClass="table  table-sm table-bordered table-condensed table-responsive-sm table-hover table-striped" runat="server">              
             <Columns>  
                 <asp:TemplateField >
                     <ItemTemplate>
                       
                         
                         <asp:LinkButton ID="viewdetail" runat="server"   Text='<%#Bind("[userid]") %>'  OnClick="btnview_Click" >
                         </asp:LinkButton>
                             
                     </ItemTemplate>
                 </asp:TemplateField>
                 <asp:BoundField DataField="username" HeaderText="userid" />
                <asp:BoundField DataField="email" HeaderText="Email" />
                <asp:BoundField DataField="mobile" HeaderText="Mobile" />
               
            </Columns>

          </asp:GridView>

CodePudding user response:

Well, first up, not clear why you using a link button as opposed to a regular button?

but, yes, a button dropped into a GV?

You actually don't have to pass anything WHEN/if the GV has a primary key value.

If you are dealing with PK's in the GV, then this will work:

Just make sure you set "datakeys" value.

This is not only nice (no need to include/put/have the PK in the GV, but it also nice for reasons of security, since then no pk "id" is ever exposed to the client side browser.

So, this markup:

        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            DataKeyNames="ID"  width="50%" CssClass="table table-hover" >
            <Columns>
                <asp:BoundField DataField="FirstName" HeaderText="FirstName"  />
                <asp:BoundField DataField="LastName" HeaderText="LastName"  />
                <asp:BoundField DataField="HotelName" HeaderText="HotelName"  />
                <asp:BoundField DataField="Description" HeaderText="Description"  />
                <asp:TemplateField>
                    <ItemTemplate>
                    <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
                        OnClick="cmdView_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

Code behind to load:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadGrid();
    }

    void LoadGrid()
    {
        using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
        {
            string strSQL =
                @"SELECT * FROM tblHotelsA ORDER BY HotelName";
            using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
            {
                conn.Open();
                DataTable rstData = new DataTable();
                rstData.Load(cmdSQL.ExecuteReader());
                GridView1.DataSource = rstData;
                GridView1.DataBind();
            }
        }
    }

And we now see/get this:

enter image description here

and the row click code is this:

    protected void cmdView_Click(object sender, EventArgs e)
    {
        Button cmd = sender as Button;
        GridViewRow gRow = cmd.NamingContainer as GridViewRow;

        Debug.Print("Row index  = "   gRow.RowIndex);
        int PK = (int)GridView1.DataKeys[gRow.RowIndex]["ID"];
        Debug.Print("Row PK id = "   PK);

        // grab cell from click - 
        Debug.Print("Hotel name = "   gRow.Cells[2].Text);

        // jump to next page
        // Response.Redirect(@"~/EditHotel.aspx?ID = "   PK);

    }

output:

Row index  = 1
Row PK id = 5
Hotel name = Inns of Banff

Now, you can also I suppose "pass" a value with the button.

Say markup is this for the button:

                    <asp:Button ID="cmdView" runat="server" Text="View" CssClass="btn"
                        OnClick="cmdView_Click"
                        CommandArgument='<%# Eval("ID") %>' />

You can of course use any valid value from the data source - even ones NOT placed on the GV.

And in code behind, then this:

        Debug.Print(cmd.CommandArgument);

so, above shows:

How to get the row index value

How to get the data keys (row PK value)

How to pass any value using command argument.

The above use of "naming" container works for listview, gridview, repeater's etc.

But, if in your case user id is a row pk value, then you MUCH better off for reasons of security to NOT include the "user id" in the markup, and set/use datakeys for this.

However, either way is fine. You are free to drop in a button, link button, image button or whatever, but the click code can remain quite much as per above.

And even better?

You don't have to mess with gridindex changed event and all that jazz. Just a simple plain jane click event, and from that you have use of "row index", the Grid row (gRow), and also use of datakeys (the PK primary key value for the row if desired).

so, if possbile, and userid is a PK row value, then use data keys.

But, you certainly can do this in the button markup:

  CommandArgument='<%# Eval("userid") %>' 

And then in the above example, use

    int UserID =  Convert.ToInt32(cmd.CommandArgument);
  • Related