如何从列表视图中获取信息

时间:2014-01-28 14:13:13

标签: c# asp.net listview

我想从列表视图中提取信息。我不知道该怎么做。

我在listview(Doctor_Name)中有4个字段来自Doctor Table,来自Appointement Table的可用appointement的日期和时间。我需要从Doctor表中捕获doctor_Id。我们可以隐藏列表视图中的字段吗?列表视图是否会查看LinqDataSource中的所有字段或仅显示我们在屏幕上定义的字段?

这是Appointement.aspx

      <asp:ListView ID="ListView1" runat="server" DataKeyNames="aptId" 
        DataSourceID="LinqDataSource1" InsertItemPosition="LastItem" >
            <AlternatingItemTemplate>
                <tr style="">
                   <td>
                      <asp:Label ID="doctorNameLabel" runat="server" 
                          Text='<%# Eval("Doctor.Doctor_Name") %>' />
                    </td>
                    <td>
                      <asp:Label ID="dateAptLabel" runat="server" 
                         Text='<%# Eval("dateApt", "{0:dd/MM/yyyy}") %>' />
                    </td>
                    <td>
                      <asp:Label ID="hourAptLabel" runat="server" 
                         Text='<%# Eval("hourApt", @"{0:hh\:mm}") %>' />
                    </td>

                    <td>
                       <asp:CheckBox id="MyCheckBox" runat="server"
                            value='<%# Eval("rdvId") %>'
                            AutoPostBack="true"
                            OnCheckedChanged="Check_Clicked" />
                    </td>
                </tr>
            </AlternatingItemTemplate>

这是Appointement.aspx.cs

protected void Appointement_Update()
{
    int keyAptId = 0;
    int indxChkBox = 0;
    string sDateApt = "";
    string sHourApt = "";

    foreach (ListViewDataItem item in ListView1.Items)
    {
       CheckBox MyCheckBox = (CheckBox)item.FindControl("MyCheckBox");
       if (MyCheckBox.Checked) 
       {
           keyAptId = Convert.ToInt32(ListView1.DataKeys[item.DataItemIndex].Value);


           Session["doctorName"] = ;  <== I want to extract the doctor name from the listview
           Session["doctorId"] = ;   <== I want to extract the doctor ID 


           Label myAptDate = (Label)item.FindControl("dateAptLabel");
           sDateApt = myAptDate.Text;
           Session["dateApt"] = sDateApt;

           Label myAptHour = (Label)item.FindControl("hourAptLabel");
           sHourApt = myAptHour.Text;
           Session["hourApt"] = sHourApt;
       }
     }

这是Appointement.dbml

Appointement Table(aptId,clientId,doctorId,dateApt,timeApt,IsAvailable)    医生表(Id,doctor_name)

感谢大家的建议。

以下是添加hiddenField的方法。

       <asp:ListView ID="ListView1" runat="server" DataKeyNames="aptId" 
          DataSourceID="LinqDataSource1" InsertItemPosition="LastItem" >
            <AlternatingItemTemplate>
                <tr style="">
                    <td>
                      <asp:HiddenField ID="lblDoctorId" runat="server" 
                         Value='<%# Eval("Doctor.Id") %>' />
                    </td>
                    <td>
                      <asp:Label ID="doctorNameLabel" runat="server" 
                           Text='<%# Eval("Doctor.Doctor_Name") %>' />
                    </td>
                    <td>
                      <asp:Label ID="dateAptLabel" runat="server" 
                           Text='<%# Eval("dateApt", "{0:dd/MM/yyyy}") %>' />
                    </td>
                    <td>
                      <asp:Label ID="hourAptLabel" runat="server" 
                            Text='<%# Eval("hourApt", @"{0:hh\:mm}") %>' />
                    </td>

                    <td>
                       <asp:CheckBox id="MyCheckBox" runat="server"
                            value='<%# Eval("aptId") %>'
                            AutoPostBack="true"
                            OnCheckedChanged="Check_Clicked" />
                    </td>
                </tr>
            </AlternatingItemTemplate>

           <ItemTemplate>
                <tr style="">
                    <td>
                      <asp:HiddenField ID="lblDoctorId" runat="server" 
                              Value='<%# Eval("Doctor.Id") %>' />
                    </td>
                    <td>
                      <asp:Label ID="DoctorNameLabel" runat="server" 
                         Text='<%# Eval("Doctor.Doctor_Name") %>' />
                    </td>
                    <td>
                      <asp:Label ID="dateAptLabel" runat="server" 
                         Text='<%# Eval("dateApt", "{0:dd/MM/yyyy}") %>' />
                    </td>
                    <td>
                      <asp:Label ID="hourAptLabel" runat="server" 
                        Text='<%# Eval("hourApt", @"{0:hh\:mm}") %>' />
                    </td>

                    <td>
                        <asp:CheckBox id="MyCheckBox" runat="server"
                            value='<%# Eval("aptId") %>'
                            AutoPostBack="true"
                            OnCheckedChanged="Check_Clicked"/>
                    </td>
                </tr>
            </ItemTemplate>

 Here is how it is coming out on the screen:   
 Doctor_Name   dateApt  HourApt      choice
               DoctorA  31/01/2014   09:00  checkbox
               DoctorB  31/01/2014   09:00  checkbox
               DoctorC  31/01/2014   09:00  checkbox

 if I removed the hiddenfield from the ItemTemplate it is coming out like this
 Doctor_Name   dateApt     HourApt      choice
 DoctorA      31/01/2014   09:00      checkbox
               DoctorB   31/01/2014     09:00     checkbox
 DoctorC      31/01/2014   09:00      checkbox 

有关如何修复它的想法吗?

1 个答案:

答案 0 :(得分:0)

您已经从ListView获取数据:

Label myAptDate = (Label)item.FindControl("dateAptLabel");
sDateApt = myAptDate.Text;

同样的前提适用于存储在这些UI元素中的任何其他数据。例如,您希望获得“name”属性,该属性已经可用:

Label myName = (Label)item.FindControl("doctorNameLabel");
sName = myName.Text;

如果您还想从UI获取“ID”,则需要先将其存储在UI中。它可能像隐藏的领域一样简单:

<asp:Hidden ID="doctorIDLabel" runat="server" 
            Value='<%# Eval("Doctor.ID") %>' />

然后,您可以继续使用您已经使用的相同技术从UI元素中获取其值。这些领域没有什么特别或不同之处,你现有的技术将适用于所有这些领域。