0

How do you retrieve a value from HTML in ASP.NET C#? I'm using a flat JSON file and fetching the data via Ajax and output the results with HTML. Normally this would work if you're using asp:ListItem.... tag. But I'm having trouble with retrieving the the value from the HTML tag upon form submission.

jQuery

    $.ajax({
        url: "js/covers.json",
        dataType: "JSON",
        success: function (data) {
            var $select = $("#customCoversDD");
            $select.empty().append('<option value="">- Please Select One -</option>');
            $.each(data.custom, function (key, val) {
                $select.append('<option id= "customCoversList" value="' + val.description + '">' + val.description + '</option>');
            });
        }
    });

ASP.NET

<asp:RadioButton ID="customCover" Text="Custom Cover" GroupName="covers" runat="server"/>
<asp:DropDownList ID="customCoversDD" runat="server">
</asp:DropDownList>

C#

string coverChoice = "";
if (customCover.Checked)
{
coverChoice = customCoversDD.SelectedValue;
};
Kluong
  • 302
  • 3
  • 15
  • What's the `$select` variable referring to? How and where did you initialize it? – Darin Dimitrov Dec 30 '15 at 19:42
  • So, what is wrong? The list does not get populated or it does but when you select a value `coverChoice` contains nothing? – Orif Khodjaev Dec 30 '15 at 19:42
  • Everything is populated. You can see the list fine. However, lets just say you select an option, I'm not able to get that Value when I submit the form. – Kluong Dec 30 '15 at 19:44
  • Is it web form or content page? If its a web form it will work.If its a content page then asp.net run time will adds some kind of string with the ID of the control.So the ID of the control will change when its rendered on the browser. So you have to find the ID of the control and change your JS code accordingly. You can use Chrome developer tool or firebug or IE developer tool to find the ID of the control. – Ramesh Dec 30 '15 at 19:55

2 Answers2

0

You're giving each option element the same id of 'customCoversList'. Try this...

$.each(data.custom, function (key, val) {
        $select.append(
              $('<option></option>').val(val.description).html(val.description)
        );
);
0

I believe this is happening due to the values of the drop down list not being in viewstate.

Try this instead: Request.Form("customCoversDD"); That would get you the selected value of the drop down list.

N8ALL3N
  • 375
  • 1
  • 2
  • 14
  • So I would just need to replace: coverChoice = customCoversDD.SelectedValue; to: coverChoice = Request.Form("customCoversDD"); ? – Kluong Dec 30 '15 at 23:52
  • @Kluong yeah, I think that code would give you what you want. – N8ALL3N Dec 31 '15 at 15:57
  • I've tried it and I'm getting this error:"Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation." Do you happen to know what's the problem? – Kluong Jan 04 '16 at 18:42
  • This post has what you need to do...similar problem, but with a listbox http://stackoverflow.com/q/228969/3147859 – N8ALL3N Jan 06 '16 at 18:34