简体   繁体   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。

Firstly, add SelectedIndexChanged event and set AutoPostBack to True on your dropdownlist control. 首先,添加SelectedIndexChanged事件,并在下拉列表控件上将AutoPostBack设置为True。

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. 然后在后面的代码中添加以下代码,仅排除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);
}

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. 注意:此行为创建一个POST ,它将触发Page_Load ,如果您在中继列表上触发下拉列表事件之前调用DataBind() ,则它将完全不触发。 Make sure that DataBind() is guarded with !IsPostback to avoid supressing the event. 确保使用!IsPostback保护DataBind()以避免抑制该事件。

for example: 例如:

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

暂无
暂无

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

相关问题 如何更新由jQuery AJAX调用添加其列表项的ASP Dropdown:错误无效的回发或回调 - How do I update ASP Dropdown whose list item are added by jQuery AJAX call : Error Invalid postback or callback 我如何从下拉列表中获取项目作为输出两次或三次,或作为用户输入多次 - How can i get item from dropdown list as output twice or thrice or any number of times as user inputs 如何从子页面触发更新面板回发? - How do I trigger an update panel postback from a child page? 如何通过视图中的javascript将选定的下拉项作为参数传递来调用发布动作响应 - How do I call a post actionresponse passing the selected dropdown item as a parameter from javascript within a view 从下拉列表中选择值时如何回发到控制器功能 - How to postback to a controller function when a value is selected from dropdown 如何避免每次回发后页面跳动? - How do I keep my page from jumping around after every postback? 如何在数据列表中调用按钮的asp回发? - how do I call the asp postback of button inside datalist? 当我在asp:DropDown中选择其他项目时,如何更改C#Gridview数据 - How do I Change C# Gridview data when I select a different item in an asp:DropDown 如何在LINQ的select函数中调用函数 - How do I call function in select function in LINQ 我想在下拉列表中选择一个项目,但每次运行时下拉列表的 id 都会改变 - I want to select an item in a dropdown list but the id of the dropdown list change with every run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM