简体   繁体   中英

How to add a button to select an option on gridview in asp.net

I am creating a website so a user can log in and access their own data (through a session) - So far when they log in and are authenticated they are redirected to the user page and can see their own data (in this case it is patients seeing their medication) - I want to provide a button that corresponds with the medicine and allows the user to select the medication so I can use this in an ordering process (if selected then insert to table) - I am not sure how to do this.

From the database there is a patient table (all tables are all made up dummy data):

患者

A Medicine table:

医学

And a link table that connects a patient to their specific medicines:

链接

This is the code that brings up the table showing a patients own particular medicine on the user page:

user.aspx

<asp:Content ID="Content3" ContentPlaceHolderID="contentbody" runat="Server" Inherits="Pages_user" CodeFile="Pages_user.aspx.vb">

<p>
   <span class="auto-style2">Please Select Your Medication&nbsp;&nbsp;&nbsp;
    </span>

</p>

<asp:GridView ID="GridView1" runat="server" ></asp:GridView>

</asp:Content>

user.aspx.vb :

Partial Class Pages_user
Inherits System.Web.UI.Page

Sub Page_Load(ByVal Sender As System.Object, ByVal e As System.EventArgs)

    If Not IsPostBack Then
        Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Laura\Final_proj\App_Data\surgerydb.mdf;Integrated Security=True;Connect Timeout=30")
        Dim cmdstring As String = "SELECT pt.PatientId, pt.ForeName, pt.Username, md.Name, md.Purpose, md.Instrcutions  " +
                                    "FROM Patient pt INNER JOIN prescription pr ON pt.PatientId = pr.PatientId  " +
                                    "INNER JOIN medicine md ON md.MedicineId = pr.MedicineId Where pt.PatientId  = @PatientId"
        Dim dt As New System.Data.DataTable()
        Dim da As New System.Data.SqlClient.SqlDataAdapter(cmdstring, conn)
        da.SelectCommand.Parameters.Add("@PatientId", System.Data.SqlDbType.Int).Value = CInt(Session("PatientId").ToString())
        conn.Open()
        da.Fill(dt)
        conn.Close()

        GridView1.DataSource = dt
        GridView1.DataBind()
    End If

End Sub

this is what appears when the user logs in (and ive drawn on what I want to display also):

在此处输入图片说明

I hope someone can help been stuck on this for a wile.

编辑网格列并添加选择命令字段,或者您可以添加按钮字段并解释click事件。

Add the following to you grid, like so:

<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:CommandField ShowSelectButton="True" />
</Columns>
</asp:GridView>

By default clicking this field will cause its corresponding row to be selected. You can then add a datakey that will tell the id or name or whatever of the selected item.

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