简体   繁体   中英

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。

Firstly, add SelectedIndexChanged event and set AutoPostBack to True on your dropdownlist control.

Then in code behind add following code and just exclude the ScriptManager part and use your method to store or display any where u like.

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

Change:

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

To:

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

Code Behind:

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

This fires the event immediately.

NOTE: This behaviour creates a POST which will fire Page_Load , if you call DataBind() on the repeater BEFORE the drop down list event has fired it will not fire at all. Make sure that DataBind() is guarded with !IsPostback to avoid supressing the event.

for example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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