To set the value of the textfield use .val()
$("#dbg").val(selectedItems.join(','));
Same for the hiddlen field also
$("#hiddenCategoryHash").val(selectedItems.join(','))
Pergunta
My scenario is this. User clicks on button and a modal dialog appears with a checkboxlist. When the user makes their selections and clicks 'ok' in client click event I want to set a hiddenfield with the list of values the user selected. The user continues with rest of form and clicks another button to submit the whole form. From code behind (button Click event) I need to access this list of values that was stuffed into the hiddenfield to save to DB.
ClienclickEvent code
function onOk() {
var selectedItems = [];
$("[id*=CategoryCheckboxList] input:checked").each(function () {
selectedItems.push(this.value);
});
//stuff the values in the hidden field for later postback
$("#hiddenCategoryHash").value = selectedItems; //appears to work, no errors
$("#debugcat").text(selectedItems); //stuff into a div for testing (works)
$("#dbg").text = selectedItems; //attempted to stuff in Textbox. FAIL
}
It should be noted that I have also tried this with an html input control and using request.form to retrieve the values that way, but does not work either. Do I have the right approach here? Should I go about this in a different manner?
Here is the markup
<asp:HiddenField ID="hiddenCategoryHash" runat="server" EnableViewState="true" />
<asp:TextBox ID="dbg" runat="server"></asp:TextBox>
<input type="hidden" name="conf4" value="test" />
Lastly here is the code behind
xfer.IsActive = True
xfer.IsDiscontinued = False
Dim hiddenval = Request.Form("hiddenCategoryHash")
Dim test = hiddenCategoryHash.Value
Dim debugdiv = Request.Form("debugcat")
xfer.CategoryIDhash = hiddenval
dc.Transfers.InsertOnSubmit(xfer)
dc.SubmitChanges()
Thank you for all your suggestions. As it turns out I had to grab the full clientID of the hidden control in my jquery selector, like so: $("#<%= hiddenCategoryHash.ClientID %>").val(selectedItems);.
Solução
To set the value of the textfield use .val()
$("#dbg").val(selectedItems.join(','));
Same for the hiddlen field also
$("#hiddenCategoryHash").val(selectedItems.join(','))
Outras dicas
Try This
function onOk()
{
selectedItems = [];
$("input[id*=CategoryCheckboxList]:checked").each(function(){
selectedItems.push($(this).val())
});
$("#hiddenCategoryHash").val(selectedItems);
$("#debugcat").text(selectedItems);
$("#dbg").val(selectedItems);
}
if you want to join the values you can use selectedItems.join(',')