textboxes are empty on postback with bootstrap modal asp.net

前端 未结 4 2151
野趣味
野趣味 2021-02-15 23:59

I am using bootstrap modal in my asp.net page like this:

   
4条回答
  •  迷失自我
    2021-02-16 00:27

    Steven Anderson is right, values are not posted because modal inputs are outside the form.

    I was able to workaround this problem using javascript to copy the value of the modal inputs to hidden asp.net controls.

    I create a hidden asp.net control outside of the modal:

    
    

    The modal textbox I want to submit:

    `
    

    My modal submit button looks like this;

    
    

    My JavaScript function looks like:

    function DoCustomPost()
    {
    var ModalTextBox = document.getElementById("FormAmenity");
    var HiddenTextBox = document.getElementById("MyHiddenControl");
    // This is the value I want to Post
    MyHiddenControl.value = ModalTextBox.value
    }
    

    So when I click "submit" in my modal window a postback kicks, the value of the textbox is copied to the hidden control and I can read the value from the hidden control in code-behind:

    Protected Sub BtnSubmit_Click(sender As Object, e As EventArgs) Handles BtnSubmit.Click
    Dim Something As String = MyHiddenControl.Value
    End Sub
    

提交回复
热议问题