FindControl TextBox which don't get the good value Sharepoint - c#

In my asp page i got :
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<asp:DropDownList ID="_SCP_ddlStatutDelais" runat="server"></asp:DropDownList>
<asp:TextBox ID="_SCP_tbTypeMiseProduction" Rows="3" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="_btSend" runat="server" Text="Envoyer" CssClass="ms-ButtonHeightWidth"
onclick="_btSend_Click"/>
</asp:Content>
Then, in my code behind, i get values from database to provide my TextBox and DDL in my Page_Load, and it works.
Then, i want to update my database with values modify by user, so i try to get the Text in TextBox but i can only got the Text that i put from my database, and myTextBox.Text ignore Text modify by user.
Code Behind :
protected void _btSend_Click(object sender, EventArgs e)
{
Control context = this.Page.Master.FindControl("PlaceHolderMain");
//Informations Database Connection etc...
reflector.Set(d[fieldtomap],rootTypeDescriptor, ref instance, ((TextBox)(context.FindControl(nodeName))).Text);
//Submit update to database
}
For example if i get from my database : "Test", i put in my TextBox "Test". Then user modify this value then validate with the button, ((TextBox)(context.FindControl(nodeName))).Text contains always "Test" and ignore user's modification.

Are you checking for Page.IsPostback when binding your data? You should only be binding on the initial page load, else, the change are overwritten - just like you are experiencing.

Related

Why label's value is changed?

I know even if the ViewState is disabled for the TextBox, we are not losing the data because it implements the IPostBackDataHandler interface.
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"/>
But my question is why this happens for label too? Why label is not losing it's data even if the ViewState is disabled since the label doesn't implements the IPostBackDataHandler interface?
<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled"/>
TextBox definition:
public class TextBox : WebControl, IPostBackDataHandler,
Label definition:
public class Label : WebControl, ITextControl
My code:
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/>
</div>
</form>
And code behind:
protected void Button1_OnClick(object sender, EventArgs e)
{
Label1.Text = "Changed.";
}
I expected to see the "Before click" in my label after I clicked the button but I see the "Changed" text in my label after I clicked the button.
Ok I deleted my previous answer, I will try to re-state it with an example.
First, as others stated, the idea of ViewState is to hold the state between postbacks, rather than during a single page load cycle, so what you're seeing is the expected behavior.
To see the difference with an example, try adding your label with 2 buttons:
<asp:Label ID="Label1" runat="server" EnableViewState="False" Text="Before click"></asp:Label>
<asp:Button ID="btn1" Text="Click" OnClick="btn1_Click" runat="server" />
<asp:Button ID="btnReset" Text="Reset" OnClick="btnReset_Click" runat="server" />
Where btn1 sets the value to "Changed", and btnReset has an empty handler.
Now with EnableViewState set to False, if you click on btn1 the page reloads, btn1_Click is executed, and the page is rendered with the label value = "Changed", if you click btnReset the page will reload again, but since view state is disabled, the label will revert to its original text "Before click"
If you set EnableViewState to True on the lable and click btn1 then btnReset, the label value will stay as "Changed", since the state is kept during postbacks
Hope that clarifies things a bit
This is going to be long and detailed.
Let's start with this markup. Almost identical to yours, with one additional button and a label. I'll explain why we need them later.
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/>
<asp:Label ID="Label2" runat="server" Text="Blah" />
<asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_OnClick"/>
</div>
</form>
And we'll use this code behind. Again, I'll explain the purpose of the second handler later on:
protected void Button1_OnClick(object sender, EventArgs e)
{
Label1.Text = "Changed";
}
protected void Button2_OnClick(object sender, EventArgs e)
{
Label2.Text = Label1.Text;
}
Let's see what happens when we initially load the page. First ASP.NET reads all the markup, and then goes through the page life cycle with all the events. In markup parsing stage Label1 gets assigned text Before click, and it is never changed later during initial load. So later during rendering phase this is what is getting rendered into HTML and sent to browser. Thus Before click is displayed on the screen. Nice and easy.
Now we click Button1. Postback occurs, which is a term that describes a request sent to the same page by one of its controls after it was initially loaded. Again, everything starts with ASP.NET parsing the markup, and again Label1 gets assigned text Before click. But then page life cycle events happen, including control event handlers. And in the handler for Button1 the text of Label1 is changed to Changed. Now here is the important thing to note: if ViewState for the Label1 was enabled, this new value would be stored in there, and so called tracking would be enabled for property Label1.Text. But ViewState is disabled, and so the new value is not stored anywhere except Label1. Next comes the rendering page stage, and since Label1.Text still holds the value Changed this is what is rendered into HTML and sent to the browser to display. Note however that this new value is not sent inside the ViewState field. As you can see, whether ViewState is enabled or not plays no role in what is displayed after this postback.
Now we'll click Button2, which would trigger another postback. Again, ASP.NET parses the markup, again Label1 gets text Before click. Then comes ViewState loading part. If ViewState for Label1.Text was enabled, it would load changed value into this property. But ViewState is disabled, and so the value stays the same. Thus, when we reach Button2 click handler, the value of Label1.Text is still Before click, which is assigned to Label2.Text. But Label2 has ViewState enabled, and so this new value for its text is stored in ViewState and sent to the client (ViewState is implemented as a hidden field on the client side). When everything gets to the browser, we can see Label1 and Label2 both displaying Before click.
And to nail it down we'll do a third postback, now clicking Button1 again. Just as during the first postback, Label1 ends up with Changed text. But what about Label2? Well, this one has ViewState enabled, so during initial markup parsing ASP.NET assigns it the value Blah, and during ViewState loading it replaces this value with Before click. Nothing else affects this value during the page life cycle (we did not click Button2 this time), and so we end up seeing Changed and Before click in the browser.
Hopefully it makes it clear what the ViewState is for and what disabling it does. If you want to dive even deeper into how ViewState works, I would greatly recommend this article: TRULY Understanding ViewState.
I think you have wrong understanding what ViewState is.
Data in ViewState is being stored BETWEEN requests, but not DURING page lifecycle.
BTW - ViewState data is generated after PreRenderComplete event in SaveStateComplete event.
https://msdn.microsoft.com/en-us/library/ms178472.aspx
If you have ViewState switched off - just think that it will not be generated in output.
During page lifecycle all data assigned to controls(and also to page fields and properties, as the page is just a class) will be rendered as you defined in aspx. But will be lost after, unless is saved in ViewState.

Bind a textbox value in a label box using c# inside a uframe

I have entered a value in text box and click the button the value should bind in a label.
The code must be in c# not in query. There is no database also.how please say the code behind in c#. It is button click event. while click the button the page should post back and bind the data how to bind a textbox value in a label box using c#. The value must bind in postback method.
easy , take a look at the below code:
<asp:TextBox Id="txt1" runat="server" />
<asp:button Id="btn1" runat="server" onClick="Button1_Click"/>
<asp:Label Id="lbl1" runat="server"/>
//in the code behind implement the Button1_Click
protected void Button1_Click (object sender, EventArgs e)
{
lbl1.Text= txt1.Text;
}
It as easy as the following:
LabelID.Text = TextBoxID.Text;
Where LabelID is the ID of your Label control and TextBoxID is the ID of your TextBox control.
You should place this code inside the button click's event.
Update
Please use for your purpose ASP.NET Web Server Controls.
<asp:TextBox ID="textBoxId" runat="server"/>
<asp:Label ID="labelId" runat="server"/>
Then you can access the values of Text property of both the above controls, like above:
labelId.Text = textBoxId.Text;

Calling a function in Aspx.cs with change in dropdownbox

I have a page where in a change in the value from the drop down box will pass the corresponding text in the drop down box to get the values from the database.
<asp:DropDownList ID="dropid" runat="server" OnChange="Getvaluesfromaspx"></asp:DropDownList>
I want the function named "Getvaluesfromaspx" in the aspx.cs to be called from the aspx file.
Please help.
Use "OnSelectedIndexChanged" event instead of "OnChange" event.
Also set AutoPostBack property value to true.
<asp:DropDownList ID="dropid" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Getvaluesfromaspx"></asp:DropDownList>
And in code behind
protected void Getvaluesfromaspx(object sender, EventArgs e)
{
//Do whatever want to do here.
}

Is this the intended behavior of textbox and label?

I set the value of the controls when the DOM loads. I have this super simple code on aspx page:
<script type="text/javascript">
$(document).ready(function () {
$('#<%=textBox2.ClientID %>').val($('#<%=textBox1.ClientID %>').val());
$('#<%=lblVal.ClientID %>').html($('#<%=textBox1.ClientID %>').val());
});
</script>
<asp:TextBox runat="server" ID="textBox1" Text="Test data" />
<asp:TextBox runat="server" ID="textBox2" />
<asp:Label runat="server" ID="lblVal" Text="Old Data" />
<asp:Button runat="server" Text="Click Me" onclick="Unnamed1_Click" />
In my button click event handler I have this code:
protected void Unnamed1_Click(object sender, EventArgs e)
{
Debug.Write(textBox2.Text);
Debug.Write(lblVal.Text);
}
The thing that came shocking to me lblVal has it's old value. Setting value in javascript doesn't really have any effect on label whereas the textbox2's data is reflected on the server. Is the intended behavior of textbox and label? This came as a bit of surprise to me because I never came across this thing previously.
The label will get rendered to an HTML label tag, not a form field. Therefore it does not have a value that is posted when the form is submitted, and the value you get restored in the postback is from ViewState. Form fields (e.g. a ASP TextBox which becomes an input) will post their value and this will override the value in viewstate.
You could get around this by instead having a hidden input which you update with javascript, and then in your postback update the label with the contents of that instead.
An asp:Label control renders to a span, and is therefore not a form element, and its changed contents will not be posted to the server.

postback via MasterPage - ASP.Net, c#

I have a master page and have included a search text box.
User is on default.aspx
user enters a search value in the search text box, which is part of master page.
Via javascript the form, in MP, is submitted to my search functionality page, search.aspx and the code behind search.aspx.cs obtains the form post and gets the data.
Search.aspx has a bare bone table to display the results.
I have a breakpoint in search.aspx.cs and am able to see the raw results returned from DB. However after all databinding has occured, the user is not directed from default.aspx to search.aspx
Don't use any javascript to submit the form, just set the PostBackUrl property of your search button to Search.aspx:
<asp:TextBox ID="TxtSearch" runat="server" />
<asp:LinkButton
ID="BtnSearch"
runat="server"
PostBackUrl="Search.aspx"
Text="Search" />
And in the Page_Load of Search.aspx read the posted value:
protected void Page_Load(object sender, EventArgs e)
{
string searchText = Request["TxtSearch"];
}

Categories