簡體   English   中英

在ASP.NET WebForms中按項目ID顯示模式

[英]Show modal by item ID in ASP.NET WebForms

我正在使用中繼器來顯示從數據庫中檢索到的項目的列表。 我想通過一個簡單的模式對話框來支持對此類項目的基本CRUD操作。

我嘗試使用onitemcommand來操縱每個項目,嘗試使用標簽來獲取ID,也嘗試在按鈕上使用OnClick ,但是沒有運氣。 這是我的中繼器的代碼:

    <div class="container-fluid">
      <div class="row justify-content-left card-group">
         <asp:Repeater  ID="DataList1" runat="server" onitemcommand="DataList1_ItemCommand">
            <ItemTemplate>
               <div class="col-md-4  " style="margin-bottom: 1em;">
                  <%--A loop through the records in database--%>
                  <div class="card border-light mb-4 ">
                     <div class="card-content " >
                        <div class="card-header ">
                           <h4 class="my-0 font-weight-normal">
                              <asp:label ID="nameTage" style="font-size:16px;color:steelblue;" Text= '<%# Eval("ProjectName") %>' runat="server"/>
                               <asp:label ID="pID" Text='<%#  DataBinder.Eval(Container.DataItem,"ProjectID") %>' runat="server" Visible="false"/>
                           </h4>
                        </div>
                        <div class="card-body ">
                           <div class="d-flex justify-content-between align-items-center">
                              <div class="btn-group">
                                 <h6 class="card-text" style="font-size:10px; color:#a51313">
                                    <span class="Text" >  <%--The project description goes here--%>                            
                                    <%-- <%= myItem["ProjDescription"].ToString() %>--%>
                                    <%# Eval("ProjDescription") %>
                                    </span>
                                 </h6>
                              </div>
                           </div>
                        </div>
                        <div class="card-footer ">
                           <%--These are not for the submit buttons, they are for the models--%> 
                           <%--Get the ID of the Project--%>


                           <%--<div class="btn-toolbar  pull-right" id="buttonID" role="group" aria-label="Basic example" style="font-size:9px; color:white; align-content:center" runat="server">--%>
                              <asp:LinkButton type="button" ID="btn_details" class="btn btn-success btn-sm m-1" aria-pressed="true" data-toggle="modal" data-target="#detailsModal" runat="server" OnClick="IDButton1_Click" CausesValidation="false" CommandArgument='<%# DataBinder.Eval(Container.DataItem,"ProjectID") %>' CommandName="Details" Text="Details"></asp:LinkButton>
                              <asp:LinkButton type="button" ID="btn_edit"   class="btn btn-success btn-sm m-1" aria-pressed="true" data-toggle="modal" data-target="#editModal"   runat="server"  CausesValidation="false" CommandArgument='<%# Eval("ProjectID") %>' CommandName="Edit" Text="Edit"></asp:LinkButton>
                              <asp:LinkButton type="button" ID="btn_delete" class="btn btn-success btn-sm m-1" aria-pressed="true" data-toggle="modal" data-target="#deleteModal" runat="server"  CausesValidation="false" CommandArgument='<%# Eval("ProjectID") %>' CommandName="Delete" Text="Delete"></asp:LinkButton>
                           <%--</div>--%>
                        </div>
                     </div>
                  </div>
               </div>
            </ItemTemplate>
         </asp:Repeater>
         <%-- <% } %>--%>
      </div>
   </div>
'''
<!-- Modal for Details -->
    <%--Get the ID of the Project--%>
   <div class="modal fade" id="detailsModal" tabindex="-1" role="dialog" aria-labelledby="detailsModalLabel" aria-hidden="true">
      <div class="modal-dialog" role="document">
         <div class="modal-content">
            <div class="modal-header">
               <h5 class="modal-title" id="detailsModalLabel">Mission Project</h5>
               <%--project.Name--%>
               <button type="button" class="close" data-dismiss="modal" aria-label="Close">
               <span aria-hidden="true">&times;</span>
               </button>
            </div>
            <div class="modal-body">              
               <div class="form-horizontal col-8">
                  <div class="form-group row">
                     <asp:label Text="Project Title:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB1" Text="" runat="server" class="form-control" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label Text="Project Description:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB2" runat="server" class="form-control" rows="4" TextMode="multiline" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label  Text="Department:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB3" runat="server" class="form-control" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label ID="HEreThere" Text="Contact Name:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB4" runat="server" class="form-control" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label Text="Launch Date:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB5" runat="server" class="form-control" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label Text="Note:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB6" runat="server" class="form-control" rows="7" TextMode="multiline" ReadOnly="true"></asp:TextBox>
                  </div>
                  <div class="form-group row">
                     <asp:label Text="Project Status:" class="control-label" runat="server"/>
                     <asp:TextBox ID="TB7" runat="server" class="form-control" ReadOnly="true"></asp:TextBox>
                  </div>
               </div>
            </div>
            <div class="modal-footer">
               <asp:Button type="button" class="btn btn-secondary" data-dismiss="modal" runat="server" Text="Close" />
            </div>
         </div>
      </div>
   </div>

    protected void Page_Load(object sender, EventArgs e)
    {
        lblMessage.Text = "";

        //data = "";

        if (!IsPostBack)
        {

        }

        DataList1.DataSource = GetProjects();
        DataList1.DataBind();
  }


    protected void DataList1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandArgument != null)
        {
            if (e.CommandName.Equals("Details"))
            {
                //int MissID = Convert.ToInt32(e.CommandArgument.ToString());
                //lblMessage.Text = MissID.ToString
                ((Label)e.Item.FindControl("pID")).Visible = true;

            }
            else if (e.CommandName.Equals("Edit"))
            {
                ((Label)e.Item.FindControl("pID")).Visible = true;
            }
            else if (e.CommandName.Equals("Delete"))
            {
                ((Label)e.Item.FindControl("pID")).Visible = true;
            }

        }
        //    Label label = e.Item.FindControl("pID") as Label;

    }

protected void IDButton1_Click(object sender, EventArgs e)
{
    RepeaterItem item = (sender as LinkButton).Parent as RepeaterItem;

    int MissID = Convert.ToInt32((item.FindControl("pID") as Label).Text);

    (item.FindControl("pID") as Label).Visible = true;
    //    get command argument here
    //    int MissID = Convert.ToInt32(((sender as Button).NamingContainer.FindControl("pID") as Label).Text);

    //    lblMessage.Text = MissID.ToString();

    DataSet dsID = GetProjectByID(MissID);


    this.TB1.Text = dsID.Tables[0].Rows[0]["ProjectName"].ToString();
    this.TB2.Text = dsID.Tables[0].Rows[0]["ProjDescription"].ToString();
    this.TB3.Text = dsID.Tables[0].Rows[0]["Department"].ToString();
    this.TB4.Text = dsID.Tables[0].Rows[0]["ContactName"].ToString();
    this.TB5.Text = dsID.Tables[0].Rows[0]["DueDate"].ToString();
    this.TB6.Text = dsID.Tables[0].Rows[0]["Notes"].ToString();
    this.TB7.Text = GetStatusText(dsID.Tables[0].Rows[0]["StatusID"].ToString());

}

因為您沒有在PostBack支票中包裹Repeater綁定,所以只要發生按鈕事件,它就會重新綁定。 這實際上會撤消您在click事件中所做的更改。 將其包裝在回發檢查中應該可以解決此問題。

if (!IsPostBack)
{
    DataList1.DataSource = GetProjects();
    DataList1.DataBind();        
}

暫無
暫無

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

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