繁体   English   中英

当用户从页面回发的下拉列表中选择任何项时,我想立即调用函数吗?

[英]I want to call a function immediately as the user select any item from dropdown on page postback how to do it?

<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
                <ItemTemplate>
                    <tr>
                        <td><%#subtypes.FindByPk(Convert.ToInt32(Eval("SubitemID"))).title%></td>
                        <td><%#Eval("quantity")%></td>
                        <td><%#ThanaRecord.FindByPk(Convert.ToInt32(Eval("Thanaid"))).title%></td>
                        <td><%#Eval("created_at")%></td>
                        <td> <% if (Employee.GetCurrentEmployee().role == "Admin") { %>
                            <a href="AddDemand.aspx?type=update&id=<%#Eval("id")%>">EDIT</a>

                                <a href="AddDemand.aspx?type=delete&id=<%#Eval("id")%>">DELETE</a>
                            <% } %>
                        <%if (Employee.GetCurrentEmployee().role == "SuperVisor")
                           { %>
                          <asp:DropDownList ID="DropDownList1" runat="server" Width="120px"  AutoPostBack="false" CssClass="form-control">
                     <asp:ListItem Text="Status" Value="0">Status</asp:ListItem>
                              <asp:ListItem Text="Accept" Value="1">Accept</asp:ListItem>
                    <asp:ListItem Text="Reject" Value="2">Reject</asp:ListItem>

                </asp:DropDownList>

                            <%--<asp:textbox runat="server" id="textTest"></asp:textbox>--%>
                        </td>
                        <%} %>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
CODE:
 protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //DropDownList DropDownList1 = (DropDownList)sender;
        //string SelectedValue = DropDownList1.SelectedValue;
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DropDownList ddldrop = (DropDownList)e.Item.FindControl("DropDownList1");
            int  value =Convert.ToInt32( ddldrop.SelectedValue);
            Supervisor sup = new Supervisor();
        if (value == 1) {
                sup.Status = "Accept";
                sup.Save();
            }
        }
    }

实现SelectedIndexChanged事件并将AutoPostBack设置为True。

首先,添加SelectedIndexChanged事件,并在下拉列表控件上将AutoPostBack设置为True。

然后在后面的代码中添加以下代码,仅排除ScriptManager部分,并使用您的方法存储或显示您喜欢的任何位置。

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
        DropDownList ddl = sender as DropDownList;
        RepeaterItem rptItems = ddl.NamingContainer as RepeaterItem;
        DropDownList ddlItems = rptItems.FindControl("DropDownList1") as DropDownList;
        ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "showname", "javascript: alert('" + ddlItems.SelectedItem.ToString()   + "');", true);
}

更改:

<asp:DropDownList ID="DropDownList1" 
                  runat="server" 
                  Width="120px"  
                  AutoPostBack="false" 
                  CssClass="form-control">

至:

<asp:DropDownList ID="DropDownList1" 
                  runat="server" 
                  Width="120px"  
                  AutoPostBack="true" 
                  CssClass="form-control"
                  OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged>

背后的代码:

// event handler - this event will fire for ALL drop downs in the repeater
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{

  // this will tell which drop down fired the event
  var dropdown = (DropDownList)sender;
  // this will tell you the repeater item containing the drop down
  var repeateritem = (RepeaterItem)dropdown.NamingContainer;
}

这会立即触发事件。

注意:此行为创建一个POST ,它将触发Page_Load ,如果您在中继列表上触发下拉列表事件之前调用DataBind() ,则它将完全不触发。 确保使用!IsPostback保护DataBind()以避免抑制该事件。

例如:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostback)
    {
        Repeater1.DataSource = SomeDataSource; // whatever
        Repeater1.DataBind();
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM