I have an asp.net dropdown like this
<asp:DropDownList width="95%" ID="RessourceComposantes" runat="server"
DataSourceID="Composantes"
DataTextField="Description" DataValueField="ComposanteID">
</asp:DropDownList>
<asp:SqlDataSource ID="RessourceComposantes" runat="server"
ConnectionString="<%$ ConnectionStrings:OraEntities %>"
SelectCommand="SELECT [Blabla], [blablabla] FROM [blablablaa]
ORDER BY [blablablabla]">
</asp:SqlDataSource>
Is it normal that when I do dd_ressource_composante.selectedvalue on the server side I have no value. It's always "".
the source looks like this :
<select name="ctl00$Tab$dd_ressource_composante" id="ctl00_Tab_dd_ressource_composante" style="width:95%;">
<option value="1">Composante</option>
<option value="3">DGAG</option>
<option value="2">DSF</option>
<option value="5">Test</option>
<option value="6">Tous</option>
<option value="4">VMD</option>
</select>
I just tried to fill my dropdown in vb.net and I have the same result. The dropdown is full but when I do a postback I have no selected value
Actualy on the server side when I put a breakpoint on the dropdown, the item count is 0. I don't understand why... It's like the 8th dropdown list in this project and everything is the same but this one doesn't work.
I don't do any binding on the page load. it's all in the aspx file
well it works ONLY when I add autopostback="true" SelectedIndexChanged="dd_ressource_composante_SelectedIndexChanged" for the dropdown.
And theres no code in dd_ressource_composante_SelectedIndexChanged
It's vb.net for the server side
Thank you
Where are you calling your dd_ressource_composantes.DataBind() method in the code-behind? Is it in the Page_Load event? If so, have you wrapped that statement in an IsPostback check so that you're not binding it everytime the page loads? Not doing so would reset your DataSource, and any selected value, every time the page posts back.
If (Not Page.IsPostBack) Then
dd_ressource_composantes.DataBind()
End If
NOT
' No IsPostBack check
dd_ressource_composantes.DataBind()
Also, doesn't your DataSourceID have to be "RessourceComposantes" and not "Composantes" since that's the ID of your datasource?
When setting SelectedValue, the value must match the value of the Value attribute. If that's not the case, the results are, for all intents and purposes, undefined.
If you are calling DataBind() method somewhere in your page_load event please comment that otherwise wrap it in
IF NOT IsPostBack Then
DataBind()
EndIF
This problem is most likely caused by rebinding the control before the event you are using is fired. If you are getting the selectedItem.Text the SelectedValue should also be there.
Well that's weird. That control was in a table and I added a row like this
tblRessourcesProjet.Rows.Insert(1, tr)
and I changed it for
tblRessourcesProjet.Rows.Add(tr)
I don't understand quite well why it was a problem but it works now.
Thank you everyone!
Related
Using ASP.NET 4.0
Bit of a strange one here, my code works but I don't know why!
So I have some HTML like so:
<asp:Repeater runat="server" ID="uxMyRepeater" ClientIDMode="Predictable">
<ItemTemplate>
<asp:Button runat="server" Text="Submit" />
<asp:HiddenField runat="server" ID="uxIsVisibleHiddenField" Value="0" />
</ItemTemplate>
</asp:Repeater>
And the back end:
Protected Sub uxMyRepeater_ItemCommand(source As Object, e As RepeaterCommandEventArgs) Handles uxMyRepeater.ItemCommand
uxIsVisibleHiddenField.Value = "1"
End Sub
So for some reason this works, usually I would expect to have to declare uxIsVisibleHiddenField in uxMyRepeater_ItemCommand like so:
Dim uxIsVisibleHiddenField As HiddenField = DirectCast(e.Item.FindControl("uxIsVisibleHiddenField"), HiddenField)
But in this particular case it works without the declarative statement. Can anyone shed any light on why it would do this?
Please note this is sample code only, not my actual code.
EDIT
Forgot to mention there is an UpdatePanel around each RepeaterItem, removing this causes Visual Studio to give me an error that'd I'd expect: 'uxIsVisibleHiddenField' is not declared. It may be inaccessible due to its protection level.
This could only happen if you have a control with the same ID that sits outside of the repeater. You won't have ID clashes because the repeater is a naming container.
Do you have any AlternatingItemTemplate ? It might be declared in that particular area and remained unnoticed.
After a lot of debugging the only thing I can say is that when I have an UpdatePanel inside the Repeaters ItemTemplate I don't need to declare the controls inside the ItemTemplate when accessing them in the DataBind event, very strange. Taking out the UpdatePanel causes complier errors so the UpdatePanel must be doing some auto hook-up between the Repeater and the controls.
Thanks for all your suggestions.
With asp.NET control dropdownlist, there is a property AutoPostBack, if it is set "True", the whole page will be posted back.
If the aspx page include a html element "select" like:
<select id="list" name="list" runat="server"
DataTextField="Name" DataValueField="ID" ></select>
and it data is filled by code-behind.
Question is: how to allow this Select have AutoPostBack function too?
The DropDownList approach adds a __doPostBack('selectelementname', 'commandname'); call to the onchange event. When you change the value, this then proceeds to post back to the server, and then the ASP.NET control processes the postback data in LoadPostData method.
HTH.
You can't apply Auto post back property for html select control.To invoke function writen inside c# code page(serverside),you need to use webservice. You can call javascript function(client side) on 'onchange' event of html select control.
I'm having trouble with a simple radio set of two radio buttons (I don't want to use a RadioButtonList [RBL] because RBL doesn't allow child controls, and in my case, if you select one option, I want to enable a textbox next to the button; yes you could hack this with jQuery to move the textbox, but that's dirty!). I would check one, submit the form (either explicitly or through AutoPostBack), and the CheckedChanged event would never fire. When the page was reloaded, both buttons would be unchecked, regardless of their initial state on non-postback load or the state before form submission.
I took out the checkbox and stripped this down to the simplest example I could come up with. I tossed EnableViewState=true all over the place just in case it was being disabled somewhere I couldn't find.
<form id="form1" runat="server" enableviewstate="true">
<div>
<asp:RadioButton ID="foo" Text="foo" runat="server" AutoPostBack="true" OnCheckedChanged="rbChanged" Checked="true" GroupName="foobar" EnableViewState="true" />
<asp:RadioButton ID="bar" Text="bar" runat="server" AutoPostBack="true" GroupName="foobar"
OnCheckedChanged="rbChanged" Checked="false" EnableViewState="true" />
<asp:Label runat="server" ID="resultLbl" />
</div>
</form>
protected void rbChanged(object sender, EventArgs e)
{
if (foo.Checked) resultLbl.Text = "foo is checked";
else if (bar.Checked) resultLbl.Text = "bar is checked";
else resultLbl.Text = "neither is checked";
}
It turns out this was because we had a custom adapter rendering the HTML for a radiobutton (or to be precise, for all System.Web.UI.WebControls.CheckBox es). Our motivation for this was because .NET will put a disabled="disabled" attribute on the LABEL and the input, which is bad HTML, and worse, is actually interpreted to mean something by IE! (Check it out for yourselves -- write up an HTML page with a label disabled="disabled" and test in FF and IE.)
We used Reflector to see what step we were mixing up vs what the real adapter did, and found that the Name attribute was being set incorrectly. Although it set all the RBs in a given group to the same Name, it was not the same Name attribute in our limited new-solution test case as it was in our custom-adapter test case. When we looked at how to generate that safely, we found that our postbacks suddenly worked!
If we get permission from the boss, we'll contribute our adapter to CSSFriendly in case anyone else has use for this sort of thing.
I've tested your exact code and it works fine. When you select either radio button, the lable is updated during postback and correct information is shown when the page refreshes??
In some cases the issue may be caused by ScriptManager in code-behind.
For e.g.
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "DisplayMessage", "javascript: HideProcessingImage();", true);
Using the same key name in different methods as above may be the issue of radio button autopostback not working.
I've faced this issue so anyone with same issue can solve their problem.
I have a situation where a DropDownList control is not posting back correctly. The AutoPost property is set to true, so the postback does happen, but the SelectedValue is not set to the correct value. In addition, the onSelectedIndexChanged event doesn't fire. The exact same code works perfect fine on an ASPX page, but does not work in a ASCX control.
I have tried all the obvious things, I hope, trying to figure this one out, but no luck so far. I have even investigated what comes back in Request.Form["__EVENTTARGET"] and __EVENTARGUMENT. __EVENTTARGET does point to the drop down list, but the argument is empty.
Can the folks of StackOverflow help lead me in the right direction to debug this issue. Of course, it is further complicated by master pages and the usual over-complication of ASP.NET. Here is the code:
<div>
<asp:DropDownList ID="testDrop" runat="server" AutoPostBack="true"
EnableViewState="true" onselectedindexchanged="testDrop_SelectedIndexChanged">
<asp:ListItem Value="1" Text="1">1</asp:ListItem>
<asp:ListItem Value="2" Text="2">2</asp:ListItem>
</asp:DropDownList>
</div>
And here is the generated html:
<select id="ctl00_MainContent_rptAccordion_ctl00_statControl_testDrop" onchange="javascript:setTimeout('__doPostBack(\'ctl00$MainContent$rptAccordion$ctl00$statControl$testDrop\',\'\')', 0)" name="ctl00$MainContent$rptAccordion$ctl00$statControl$testDrop">
<option value="1" selected="selected">1</option>
<option value="2">2</option>
</select>
This often happens when you re-poulate the DropDownList on the postback. This invalidates any selection. In your case, it could be that the ascx control is being recreated. Are you using LoadControl(...) to load your user control?
I have Dropdownlist on my page and its selectedindexchanged method created in code behind file (.cs).
I wanted to create fake postback with A tag (onmouseover event).
First i viewed source of html.
<select name="ctl00$cpholder_ana$ddlFaturaNolar" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)" id="ctl00_cpholder_ana_ddlFaturaNolar">
<option selected="selected" value="CHOOSE"></option>
<option value="001926">[ 30.04.2009 - 156.492,00 TL ] 001926</option>
</select>
Then, I copied
onchange="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)"
And, I created A tag with mouseover event(to make Postback but as it fired by Dropdownlist)
<a onmouseover="javascript:setTimeout('__doPostBack(\'ctl00$cpholder_ana$ddlFaturaNolar\',\'\')', 0)">asdasdasdasdad</a>
But it didn't drop to SelectedIndexChanged method.
First, WHY?
Second, How can i do this?
Thanks from now.
You can use this code snippet -
__doPostBack('<%= dropdownlist.UniqueID %>', '');
You can't use hard-coded unique ids because they may change due to many reasons. For e.g. the id will change if the parent control's id changes, etc. You will have to get the UniqueID rendered from the server side using the code like the one given above.
EDIT: Forgot to mention one important thing. The page will postback only when the selectedIndex of the dropdown changes :) So, if you want to fire that event, change the index of the dropdown list using this and then call the __doPostBack code -
document.getElementById("<%= dropdownlist.UniqueID %>").selectedIndex = 1;
__doPostBack('<%= dropdownlist.UniqueID %>', '');
EDIT2: Adding upon what Bob said, you can make use of hidden server controls. I suggest you use a asp:Hidden control and hook up its OnValueChanged event. So, whenever you want to post your page back to the server, you just have to change the value of your hidden variable. This way you won't have to use a hidden button.
document.getElementById("<%= hiddenField.UniqueID %>").value = (new Date()).getTime();
I am sorry to say guys
__doPostBack is not working for DropDownList but it is working for Button. So do one thing
Add a button which should be followed like this
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged">
<asp:ListItem Text="All" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="Published" Value="1"></asp:ListItem>
<asp:ListItem Text="Pending" Value="2"></asp:ListItem>
<asp:ListItem Text="Rejected" Value="3"></asp:ListItem></asp:DropDownList>
now call your __doPostBack method
__doPostBack('<%= btnRefresh.UniqueID %>','');
Is it not <%= ddlFaturaNolar.ClientID %>?
Another option you can try, which I find easier and is like likely to break, is to create a hidden (style="display:none" don't use the visible property) asp:Button on your page. When you want to post back you can just simulate the click on that button
document.getElementById("<%= Button1.ClientID %>").click();
If you want to stick to posting back on the drop down, make sure the AutoPostBack property of the dropdown is still set to true. Just keep in mind this behavior is a little strange, you are firing a selected changed event on a mouse over. Not only is this a little confusing, you also are increasing the risk of accidental postbacks, as it is very easy to mouse over something and expect nothing to happen.