[英]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 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
</span>
</p>
<asp:GridView ID="GridView1" runat="server" ></asp:GridView>
</asp:Content>
user.aspx.vb : 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): 这是用户登录时出现的内容(并且我也根据我要显示的内容绘制了IVE):
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.
然后,您可以添加一个数据键,该键将告诉ID或名称或所选项目中的任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.