skip to Main Content

anyone know how does it cause ASP.NET codebehind can’t acquire the selected value.

What I found is the problem is the javascript function;

set attribute portion:

process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
process.options[process.selectedIndex].setAttribute("disabled", "disabled");

If I remove these setAttribute portion, ASP.NET codebehind can acquire the selected value.

Any solutions? If I insist want to use the setAttribute portion

Sample:

HTML

    <html>
    <table style="margin-left: auto; text-align: center; margin-right: auto; font-size: large; width: 50%" border="1">
                <tr>
                    <td class="auto-style1">Process Name</td>
                    <td class="auto-style2">:</td>
                    <td class="auto-style3">
                        <asp:DropDownList ID="ddl_processname" runat="server" CssClass="ddl-style" onchange="focustester()">
<asp:ListItem Value="P1">Process 1</asp:ListItem>
                        <asp:ListItem Value="P2">Process 2</asp:ListItem>
                        <asp:ListItem Value="P3">Process 3</asp:ListItem></asp:DropDownList>
                        <br />
                        <br />
                        <asp:Label ID="lbl_statio" runat="server" Text=""></asp:Label>
                    </td>
                </tr>
                <tr>
                    <td class="auto-style1">Tester Name</td>
                    <td class="auto-style2">:</td>
                    <td class="auto-style3">
                        <asp:TextBox ID="txt_testername" runat="server" AutoPostBack="true" OnTextChanged="TextBox_TextChanged" CssClass="txt-style"></asp:TextBox>
                    </td>
                </tr>
            </table>
        </div>
        <script>
            function focustester() {
                var process = document.getElementById('<%= ddl_processname.ClientID%>');
    
                if (process.value != "") {
                    process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
                    process.options[process.selectedIndex].setAttribute("disabled", "disabled");
    
    
                    document.getElementById('<%= txt_testername.ClientID%>').focus();
                }
            }
        </script>
        </html>

ASP.NET code behind

protected void TextBox_TextChanged(object sender, EventArgs e)
        {
            TextBox txt_ID = (TextBox)sender;
            string get_certain_box = "";

            if (ddl_processname.SelectedValue.ToString() == "")
            {
                Response.Write(@"<script language=javascript>alert('Please select Process first...')</script>");
                txt_ID.Text = "";
                ddl_processname.Focus();
            }
            else
            {
                if (txt_ID.Text.ToString() == "")
                {
                    get_certain_box = (txt_ID.ID.ToString() == "txt_testername") ? "Tester field" : "txtID";
                    Response.Write(@"<script language=javascript>alert('" + get_certain_box + " cannot blank...')</script>");
                    txt_ID.Focus();
                }
                else
                {
                    if (txt_ID.ID.ToString() == "txt_testername")
                    {
                       Response.Write(@"<script language=javascript>alert('success')</script>");
                    }

                }
            }
        }

2

Answers


  1. I maybe wrong but I remember that code behind can’t get data if the dropdown is disabled or something.
    So can you just disable dropdown after it go to focustester() ?
    but if you insist to use JavaScript then why not workaround like insert value into hidden field when using setAttribute?

    Login or Signup to reply.
  2. Add a hidden field. And then assign dropdown value to hidden field in javascript. In the server side access the hidden value for dropdown value.

    <asp:HiddenField ID="hdnProcess" runat="server" />
    

    Javascript

    function focustester() {
                    var process = document.getElementById('<%= ddl_processname.ClientID%>');
        
                    if (process.value != "") {
                        process.options[process.selectedIndex].setAttribute("style", "color:red;font-weight:600;");
                        process.options[process.selectedIndex].setAttribute("disabled", "disabled");
                        
    
                        var hdn = document.getElementById('<%=hdnProcess.ClientID%>');
                        hdn.value = process.value;
    
        
                        document.getElementById('<%= txt_testername.ClientID%>').focus();
                    }
                }
    

    Server Side

    protected void txt_testername_TextChanged(object sender, EventArgs e)
        {
            {
                TextBox txt_ID = (TextBox)sender;
                string get_certain_box = "";
                string ddlval = hdnProcess.Value.ToString();
    
                if (ddlval == "")
                {
                    Response.Write(@"<script language=javascript>alert('Please select Process first...')</script>");
                    txt_ID.Text = "";
                    ddl_processname.Focus();
                }
                else
                {
                    if (txt_ID.Text.ToString() == "")
                    {
                        get_certain_box = (txt_ID.ID.ToString() == "txt_testername") ? "Tester field" : "txtID";
                        Response.Write(@"<script language=javascript>alert('" + get_certain_box + " cannot blank...')</script>");
                        txt_ID.Focus();
                    }
                    else
                    {
                        if (txt_ID.ID.ToString() == "txt_testername")
                        {
                            Response.Write(@"<script language=javascript>alert('success')</script>");
                        }
    
                    }
                }
            }
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search