簡體   English   中英

如何使用事件ListView1_SelectedIndexChanged從linqdatasource更新listview

[英]how to update listview from a linqdatasource using the event ListView1_SelectedIndexChanged

我有一個linqdatasource女巫執行ListView1列表。 當用戶從下拉列表中選擇醫生姓名時,列表視圖將更改。 我附加了一個OnSelectedIndexChanged =“ ListView1_SelectedIndexChanged”事件。 listview1的列是apointmentId,doctorName,dateApointment,clientName和復選框。

我想更新復選框所選擇的行(基本上,用戶選擇了要約會的日期)。

 <asp:LinqDataSource ID="LinqDataSource1" runat="server" ContextTypeName="ApointmentDataContext" 
  EnableDelete="True" EnableInsert="True" EnableUpdate="True" EntityTypeName="" 
  TableName="Apointment" AutoGenerateWhereClause="True">
            <WhereParameters>
               <asp:ControlParameter 
                   Name="doctorName" 
                   ControlID="DropDownList1" 
                   PropertyName="SelectedValue"
                   Type="String" />
             </WhereParameters>

 </asp:LinqDataSource>

 <div class="center">
    <asp:Label ID="lblDoctorName" runat="server" Text="Choose a doctor name"> </asp:Label>
    <div class="value-right"> 
       <asp:DropDownList ID="DropDownList1" runat="server" Width="180px" AutoPostBack="true" >
                    <asp:ListItem Text="Doctor A" Value="Doctor A" />
                    <asp:ListItem Text="Doctor B" Value="Doctor B" />
                    <asp:ListItem Text="Doctor C" Value="Doctor C" />
       </asp:DropDownList> 
    </div>  
 </div>   <br/><br/>

 <asp:ListView ID="ListView1" runat="server" DataKeyNames="apointmentId" 
    DataSourceID="LinqDataSource1" InsertItemPosition="LastItem"    
    OnSelectedIndexChanged="ListView1_SelectedIndexChanged"  >
    <AlternatingItemTemplate>
      <tr style="">
         <td>
            <asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
         </td>
         <!--<td>
                <asp:Label ID="ApointmentIdLabel" runat="server" Text='<%# 
                   Eval("ApointmentId") %>' />
             </td>   -->
         <td>
            <asp:Label ID="doctorName" runat="server" Text='<%# Eval("doctorName") %>' />
         </td>
         <td>
            <asp:Label ID="dateLabel" runat="server" Text='<%# Eval("dateApointment", "{0:dd-MM-
              yyyy}") %>' />
         </td>
         <td>
            <asp:Label ID="ClientLabel" runat="server" Text='<%# Eval("clientName") %>' />
         </td>
         <input id="MyCheckBox" value='<%# Eval("apointmentId") %>'
                             type="checkbox" runat="server" />

以下代碼將列出所選醫生的所有可用日期。 目標是用戶將選擇一個可用日期(通過選中復選框)。 現在,我希望能夠更新指向表中的這一行。 我缺少能夠使用ListView1屬性的代碼。

protected void ListView1_SelectedIndexChanged(object sender, EventArgs e)
{
    using (ApointmentDataContext db = new ApointmentDataContext())
    {
       Listview1.
    }       
}

當我在此處放置ListView1時,出現錯誤消息名稱ListView1在當前上下文中不存在。

我希望能夠檢索guid或ApointmentId以能夠更新所選行。

通過執行此操作,我得到了復選框選擇。 也許我們可以在這里做點什么?

protected void btnSubmit_Click(object sender, EventArgs e)
{
    int iCptCheckBox = 0;
    int indxChkBox = 0;

    foreach (ListViewDataItem item in ListView1.Items)
    {

        var chk = item.FindControl("MyCheckBox") as System.Web.UI.HtmlControls.HtmlInputCheckBox;
        if (chk != null && chk.Checked)
        {
            indxChkBox = Convert.ToInt32(chk.Value);
            iCptCheckBox++;
        }
    }
 }

感謝您的幫助,以指導我解決問題。

我相信您在這里有兩個問題:

  1. 確保您的列表視圖在后面的代碼中不可見,要解決此問題,請清理頁面designer.cs文件並在aspx中進行一些小的更改(可能會添加空格),然后重新生成設計器文件。 這應該可以解決此問題。
  2. 第二個問題是獲取所選項目,您應該使用listview的SelectedDataKey屬性獲取所選ID。 我可以看到您有約會ID作為數據鍵名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM