How to make HtmlSelect Control with OnChange event to trigger C# code behind function

后端 未结 4 737
走了就别回头了
走了就别回头了 2021-01-14 06:30

How should I make \"Html Select Control\" with OnChange event to trigger C# code behind function
like ASP.NET SelectedIndexChanged of the DropDownList C

相关标签:
4条回答
  • 2021-01-14 06:41

    I've found a great way to handle my problems, showing my ideas as following code

    Front End

      <select id="StartDate"  onserverchange="StartDate_ServerChange" runat="server">
      </select>
    

    Back End

    protected void Page_Load(object sender, EventArgs e)
    {     
       ClientScriptManager cs = Page.ClientScript;
       this.StartDate.Attributes.Add("onchange", cs.GetPostBackEventReference(this.StartDate, this.StartDate.ID));
    }
    protected void StartDate_ServerChange(object sender, EventArgs e)
    {
    
    }
    

    PS: two references
    https://msdn.microsoft.com/en-us/library/ms153112(v=vs.110).aspx
    https://blog.csdn.net/lovegonghui/article/details/51942241

    0 讨论(0)
  • 2021-01-14 06:48

    Use Ajax call in xx_ServerChange function from JavaScript

    $.ajax({
        type: 'POST',
        url: 'PageName.aspx/functionName()',
        data: '{ }',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function(msg) {
            // Do something interesting here.
        }
    });
    
    0 讨论(0)
  • 2021-01-14 06:49

    If I'm getting it right, you need to use another control to trigger the code behind. Just add the button and access the state of the select

        <select runat="server" id="xx">
            <option>A</option>
            <option>B</option>
            <option>C</option>
        </select>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    

    Code behind:

        protected void Button1_Click(object sender, EventArgs e)
        {
            int index = xx.SelectedIndex;
        }
    
    0 讨论(0)
  • 2021-01-14 06:57

    I think you need to submit the form for the change to be registered:

    <form id="myForm">
        <select runat="server" id="xx" onchange="myForm.submit()" onserverchange="xx_ServerChange">
         <option>A</option>
         <option>B</option>
         <option>C</option>
        </select>
    </form>
    
    0 讨论(0)
提交回复
热议问题