0

我在一页中有一个gridview,如下所示。

 <asp:GridView ID="gvDoctorList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" 
                            AllowPaging="True" AllowSorting="True" AutoGenerateEditButton="true" AutoGenerateDeleteButton="true">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox runat="server" ID="chk" OnCheckedChanged="chk_CheckedChanged" />
                                        <asp:Label runat="server" ID="lblPID" Visible="false" Text='<%# Eval("PatientId") %>'></asp:Label>
                                    </ItemTemplate>

                                </asp:TemplateField>

                                <asp:BoundField DataField="PatientId" HeaderText="PatientId" SortExpression="PatientId" />
                                <asp:BoundField DataField="firstname" HeaderText="firstname" SortExpression="firstname" />
                                <asp:BoundField DataField="lastname" HeaderText="lastname" SortExpression="lastname" />

                                <asp:BoundField DataField="sex" HeaderText="sex" SortExpression="sex" />

                            </Columns>
                        </asp:GridView>
                        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyDatabaseConnectionString %>" 
                            SelectCommand="SELECT [PatientId],[firstname], [lastname], [sex] FROM [PatientDetails]"></asp:SqlDataSource>

现在,当用户同时选中复选框时,我希望将来自 gridview 的 PatientId 放入另一个页面,并进一步将 PatientId 插入到 sql 表中。

非常感谢有人给我代码

*更新 :: *

我的 page_load 代码是

if (!IsPostBack) { 
foreach (GridViewRow row in gvDoctorList.Rows)
 { 
   CheckBox chk = (CheckBox)row.FindControl("chk"); 
   if (chk.Checked) 
    { 
     string patientId = ((Label)row.FindControl("lblPID")).Text; 
     string exam = ((Button)sender).Text; 
     Session[patientId] = patientId;
     }
  }
 }
4

1 回答 1

1

你为什么不试试这个??

Session ["PatientID"] = YoureGridView.SelectedRow.Cells[indexOfPatientID].Text;

这样,当转到其他页面时,PatientID 的值在会话中,您可以使用在会话中检索数据

Int PatientID = Convert.Toint16(Session["PatientID"]); 

或者如果它是一个字符串

   string PatientID = Session["PatientID"].ToString();

好吧,我在猜测,因为您没有提供足够的信息,但如果我要将数据传输到另一个网页,这就是我编码的方式

为了插入 SQL 并假设它的 TSQL 我使用这样的东西

        SqlCommand Update = new SqlCommand();
        Update.Connection = YourConnection;
        Update.CommandType = CommandType.StoredProcedure;
        Update.CommandText = "usp_YoureStoredProcedure";
        Update.Parameters.AddWithValue("@id", Convert.Toint16(Session["PatientID"])); //the convert varies on what data type you are using this is just an example

好吧,这只是示例,如果您想要更明确的答案,请尝试发布或告诉我们更多信息。

于 2013-10-08T08:52:29.273 回答