【问题标题】:Get value from Gridview into the another page从 Gridview 获取价值到另一个页面
【发布时间】:2013-10-08 08:29:27
【问题描述】:

我在一页中有一个网格视图,如下所示。

 <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;
     }
  }
 }

【问题讨论】:

  • 将该父 ID 放在会话变量中并在下一页使用它/或者您可以在转到另一页时将其作为查询字符串传递:)
  • 在页面加载时将会话放在哪里?
  • 您可以根据自己的使用情况在任何地方使用使用会话
  • 你能检查一下我在页面加载中的代码是否正确?#
  • 现在如果我想在另一个页面中显示这些值该怎么办?

标签: c# asp.net gridview sql-server-2012


【解决方案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

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

【讨论】:

  • 但是当我尝试把 Session ["PatientID"] = YoureGridView.SelectedRow.Cells[indexOfPatientID].Text;
  • 我无法获取 .Cells[indexOfPatientID].Text...所以您能告诉我可能是什么问题吗?
  • -_- 当然不行,您需要在 [indexOfPatientID] 中插入患者 ID 的索引,例如 [1],[2]
  • 它也应该在一个事件中,比如说 Gridview 的 CheckBoxCheckChanged 事件或 sElectedIndexChange 事件。
  • 我已将以下代码放入 CheckBoxCheckChanged 事件保护 void chk_CheckedChanged(object sender, EventArgs e) { Session["patientid"] = gvDoctorList.SelectRow.Cells[1].Text;仍然是 selectrow 中的错误。 ?现在该怎么办?
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
  • 1970-01-01
相关资源
最近更新 更多