Control lost his value on post - c#

I got a select with the jquery uniform apply on. This select is in a form. When the form is post, the SelectedValue of my control is totally empty.
I tried to call $.uniform.update(); on the change event, but it haven't work.
The weirdest thing in this case is that when I call $("mySelect").val() the return value is the good one.
Can someone help me on this one?
Edit1: The value ( $("#mySelect").val() ) of the control is good even without doing the uniform update command. The problem is realy in passing the value to my C# code.
Edit2: I just found that the selected value is in fact always the first element of the select.
Edit3: I try to remove the aristo from the control and the selected value is still the first children in the select.

If it is ASP.net WebForms appilication, you have to check:
Your control is marked as server ( runat="server" )
You have correspodning protected field on Page ( protected TextBox myTextBox; )
ViewState is enabled

Try adding your ID in the parenthesis. I know it's not necessary, per their documentation, but I've always had success that way.
$.uniform.update("#mySelect");

Related

"Control with id could not be located or a different control is assigned to the same ID after postback." Error

I have a web-application in .NET framework 4.5 with code behind C#.
I am using a RadGrid of telerik Asp.Net Ajax control. I am creating it dynamically from code behind in PageInit event and adding it to a div that is statically defined in aspx file.
Now, for each row I am having a button that is used to expand and collapse the row using JQuery script.
I also have a RadCalendar on the page and I am fetching records according to selected date range.
Now, the problem is, if I select a date range from 1st to 16 days, it works fine. If I select 1st to 30 days then also there is no problem. But now if I select 1st to 8, it gives me the error show below :
Server Error in '/' Application.
An error has occurred because a control with id 'ctl00$ContentPlaceHolder1$gvGridViewDemo$ctl00$ctl09$Detail20$ctl06$Detail10$ctl04$ctl10' could not be located or a different control is assigned to the same ID after postback. If the ID is not assigned, explicitly set the ID property of controls that raise postback events to avoid this error.
I am NOT using any ajax in this page, so on date selection it gets full postback. Also I had set viewstate to false, ViewstateMode to disable and ClientId = autoId for both - Button and Gridview. But I am not able to get rid of this.
Can anyone help me to get rid of this ridicules error?
P.S. I have checked the link An error has occurred because a control with id {0} could not be located or a different control is assigned to the same ID after postback and according to the comment I am having string.format() in my code. But I have commented it and still I am facing the same issue.
Resolved it by myself. I do not consider it to be the great solution I have discovered but I am answering this question that it might help someone who wants to get rid of this kind of issue and tired of finding the exact solution.
I have solved it by debugging skills with Trial and Error methodology (it requires so much patience in oneself).
What I did is, I started commenting some of the code that places dynamic controls on the page and checked for result whether the same error is generated or not? If yes, then you are commenting wrong code. So just remove the comments, make that portion as it was and move on.
At last I found that there were some dynamically generated tool-tips; I was used to display some additional details on every cell of the grid view (RadGrid), were creating this error. On PageInit I was clearing all of the controls of the division and adding a grid view by code behind. So each time the request gets initiated, it clears all of the controls and adds them newly.
However, the objects of tool-tip (I have used RadTooltip to display the tool-tip on cell) were not getting cleared from the page across the postbacks.
So I just applied a new ID to each tool-tip using its cell's (cell of grid) client id + DateTime.Now.Ticks.ToString(), and I kicked off myself from this weird error.
I see you solved it, yet this may be useful in the future if you have problems with RadTooltip, I had a couple of issues resolved with this: http://www.telerik.com/help/aspnet-ajax/tooltip-troubleshooting-common-issues.html.
I was getting this problem too. What fixed it was explicitly adding ID values to all the Control objects I was dynamically loading onto my screen.
It happens for me when I have multiple command field in my grid view. I converted them to template fields and everything worked fine!

control in control losing textBox value - probably because of repeater

I have a problem: I have a user control in which there is a nested second user control. In that second control I have a textBox. During postback the value of the textbox is always the same (default one), even if I change text in that textBox.
Is this a common problem?
I just noticed that it might me caused bye repeater cause textBox is inside repater. Do you know how to foce textBox to keep value beetween postbacks if its in repeater ?
No, this is not a common issue. You should check where the control is being added and were the default value is being set. Either one could be causing you troubles.
This is not the problem, it is in your code. I guess your value is repopulating the old one in the Page_load event of the usercontrol.

Cant set drop down list selected value in page load

I'm trying to set the selected value of a ddl during page load ie. before databind.
This causes "selected value does not exist" errors. So I force a databind, and add a new element if it does not exist in the data source.
However it looks like when the databind is performed later in the page lifecycle that my added element(s) are removed/overwritten.
Am I setting the values in the wrong part of the life cycle?
what I'm doing seems rather hackish and I think im going about this the wrong way... is there a better way to do this?
Dont do it on PageLoad do it on the DataBound event of the ddl
Did you consider the OnPreRender event of the DDL... I think you will have everything to set the selected value there
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
That is to be expected, databinding clears out the items and rebinds them again. You should look at what points in the page lifecycle you are calling DataBind and also attempting to set the Selected Value.
Have you considered Page_PreRender to set the SelectedValue? This fires after all the initialisation is done, last thing before the page is rendered to the browser. (Hopefully you won't be doing any databinding in Page_PreRender ;))
But it does not seem very logical to be setting the SelectedValue in one place only for it to be overwritten again, you should only be setting the SelectedValue once - after the final .DataBind()
However it looks like when the
databind is performed later in the
page lifecycle that my added
element(s) are removed/overwritten.
As bgs264 says, that's the behaviour of databinding by design. However, if you set the AppendDataBoundItems attribute to true for your DropDownList, this won't happen, your manually added item will remain in place.
<asp:DropDownList runat="server" id="MyDropDownList" AppendDataBoundItems="true" />
My work around to this solution is as follows:
In Page Load:
Page_Load(..)
{
if(...)
{
hidCGroup.value = objCG.CName;
}
}
In DataBound:
ddlContGroup_DataBound(..)
{
ddlContGroup.Items.Insert(0, "--Select--");
ddlContGroup.SelectedIndex = ddlContGroup.Items.IndexOf(ddlContGroup.Items.FindByText(hidCGroup.Value));
}
Now there are two things to take care of. When you are using FindByText and FindByValue always take special care of the value which you are selecting from the ddl.
Sometimes, we use a numeric item as the DataValue and a text item as the DataText, when that happens, you need to interchange FindByText and FindByValue so that the proper selection is made.
Hope this helps.
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByValue(ExampleID.ToString()));
or
ddlExample.SelectedIndex=ddlExample.Items.IndexOf(ddlExample.Items.FindByText(ExampleText.ToString()));

drop down .net c#

I have a drop down that is NOT keeping the value that I select. I already check to True the EnableViewState and nothing yet.
What may be missing here? Any advise is appreciated.
If you're filling it in the Page_Load(), it will get overwritten each time the page loads. If you want the user selection to persist, fill it in the Page_Init(). The viewstate is applied in between the Page_Load and the Page_Init, so this will ensure proper order of execution.
For more info, look up the Page Lifecycle for ASsp.Net.
Check your Page_Load method. Make sure when you populate it and select the default value it is inside an
if(!IsPostBack) { .. . . }
You could be accidentally setting it on each post back, which is why it seems like it is not retaining its value.
where are you binding your Dropsown list? Make sure that you do following:
if(!IsPostBack)
{
//Do your data binding here
}

C# Web reportviewer create link to page with id

I have a reportviewer and i want a field to act as a hyperlink. The hyperlink must look like: page.aspx?id=1 But how do i achieve this?
I have entered in the properties window, navigation tab, radio "Jump to URL": page.aspx?id=sum(Field!field.value)
This doens't work :(
What do i have to do to get this work?
Thnx in advance
Martijn
PS: I also have EnableHyperlinks set to true;
Your expression under "Jump to URL" should be:
="page.aspx?id=" & sum(Fields!field.value)
Although I see 2 potential issues with that. First of all, if I remember correctly, the URL must be an absolute path (e.g. http://www.test.com/page.aspx). Secondly, I'm not sure why you're summing on a field. If you mean to only get the "current" value of some field, you don't need the aggregate function, but you have to be sure you are inside a control that repeats data for each row of a dataset, e.g. a detail row of a table.

Categories

Resources