Changed text not posting back to code-behind - asp.net

I have a textbox on an ASP.NET 4.0 page. I can add text to the textbox in the code-behind and when the page renders, the text displays just fine. However, when I modify the textbox in the browser and submit the page, the MyTextBox.Text property still shows the original text and not the modifed text I entered in the browser. How can the code-behind show the modified text?

You need to check for a postback in your initial textbox, like this:
if(!Page.IsPostBack)
{
MyTextBox.Text = "This is the text when page renders";
}
You can set your textbox normally in the code behind for the submit button handler.
void SubmitButton_Click(Object sender, EventArgs e)
{
MyTextBox.Text = tbUserInputHere.Text;
}

I doubt you took care of the page's IsPostBack condition properly.
You have to assign it a value like...
if (!IsPostBack)
{
TextBox1.Text = "You Text"
}
You are assigning a value in the page load event and when you click the button, the page load is called before the Click event handler and your value will be reset to the old value.

Related

textbox controls are display the main page in asp.net

I have designed the basic controls application in asp.net.---> Using the 3 controls are display the choosing controls.---->controls are added on the dropdownbox.-----> Values are enter the textbox.-----> Click to the button.
I have click the button is to show on the choosing controls are displayed it.
i had attached the snapshot.
enter image description here
If I get it correctly, you want to display appropriate asp.net control in a rectangle as displayed in the image?
Basically, I would go about it like this:
Add placeholder on main page. This placeholder will be used to store conrols.
Handle button click event in which you create desired controls and add them to placeholder
Main page addon:
<asp:Placeholder runat="server" ID="phControls"></asp:Placeholder>
and where button is, you need to add OnClick event handler
<asp:Button runat="server" ID="btnAddControl" ... OnClick="btnAddControl_Click"></asp:Button>
Main page code-behind:
...
protected void btnAddControl_Click(object sender, EventArgs e) {
if (myDropDown.SelectedValue == "TextBox") {
var tb = new TextBox();
tb.Text = tbEntry.Text;
phControls.Controls.Add(tb);
}
...
}
Something like that. Would prefer more information and some of your code though.

AsyncPostBackTrigger not firing on load of user control

I have an update panel which refreshes a user control. The update panel is set to conditional, and the condition is on a textbox control. Using AsyncPostBackTrigger, to fire when the text changes in the textbox, it works fine. However, what I am not getting is when the page is first loaded, and the textbox is populated with data is that it is not firing the postback trigger.
Is there a way of forcing it fire when the page is loaded for the first time?
I have tried simple things such as UpdatePanel.Update(), or txtbox.text = string.empty to force it to recognise a change- but no luck.
Any ideas anyone?
Thanks
Please replace with your ids
private void tb_Loaded(object sender, RoutedEventArgs e)
{
TextStateChanged((TextBox)sender);
}
UpdateSite.txtPhone.Loaded += new RoutedEventHandler(tb_Loaded);

Missing selected value from RadioButtonList after PostBack

First the page loads and shows an empty textbox and a button "GO"
Upon clicking the button "GO" a radioButtonList already in the page is loaded from a table based on the text in the textbox.
The radioButtonList is shown with new button "Made my choice".
The user chooses a button and clicks "Made my choice".
Upon checking for selected value or index the radiobuttonList is not checked at all...
That is it
TVM
Ricardo Conte
If you are not using Page.IspostBack property into your Page_Load then Try to use it
if(!Page.IsPostBack)
{
// Your Code..
}
into your Page_Load.Check MSDN
Hope it works for you.
protected void Page_Load(object sender, EventArgs e)
{
// Your code execute always
if(!Page.IsPostBack)
{
// Code which is execute without postback
}
}

hyperlink in gridview

I have a hyperlink in a gridview which I want users to click on and it directs them to a particular page and also either passes in the first field of the gridview (the ID) or holds it in session, preferebly in session.
The link is just static text so no matter what record they click on i want to get them to the same page, but with that records ID available.
Just not sure how to add this to the NavigateUrl of the hyperlink.
ANy hints appreciated, thanks
You can easily generate the URL in the markup of your GridView without resorting to code. What you need to do is:
In the DataNavigateUrlFields
property of your HyperLinkField, put
the name of the column that contains
your id.
In the
DataNavigateUrlFormatString, put the
path to your page, plus the
querystring that the next page will
use to get the id, but where the
value should go, put {0} instead.
e.g.
<asp:Hyperlink DataNavigateUrlFields="ProductId" DataNavigateUrlFormatString="details.aspx?id={0} />
When the control is rendered at runtime, you will find that for each row, the {0} is replaced by the value of the ProductId column.
See String.Format and DataNavigateUrlFormatString for more details.
You can handle the Row_DataBound event to find the hyperlink control and update the NavigateUrl property.
You can add simple html control Text to link, it will produce same html.
Use HyperLink control and then write an event handler function for RowDataBound event, like this:
protected void OnRowDataBound(object source, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HyperLink hyperLink = e.Row.FindControl("hyperLinkID") as HyperLink;
// example, adjust this to your needs.
hyperLink.NavigateUrl = "~/detail.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "ID");
}
}
Not sure why you have taken server control instead of tag of HTML.
two ways you can do it.
1)if it is an static link just prefix the page name and append the id in mark up.
for ex
<a href='myPage.aspx<%#Eval("YourID")%>'><strong>Click me to navigate</strong></a>
2)give some id to the a tag and make it runat server handle the data bound event and bind the value to it.
protected void MyGridview_ItemDataBound(object sender, ListViewItemEventArgs e)
{
HtmlAnchor AncImage = e.Item.FindControl("AncImage") as HtmlAnchor;
AncImage.href="myPage.aspx"/id=" + DataBinder.Eval(e.Row.DataItem, "ID"); ;
//the id is the value that you want to append for redirection
}

clear textbox after postback

I have a textbox and a button on the ascx page.
After entering text i click on the button where it will post to the database and clear the textbox.
After positing, if i refresh the web page it is going to the button click method and posting the old text to the database. how can i clear the textbox value from the view state.
i have set the enableviewstate of the textbox to false. But still it is not working. Please let me know. Thanks
protected void Button_Click(object sender, EventArgs e)
{
var txt=textBox1.Text;
textBox1.Text="";//Set it to empty
// do other stuff
............
............
}
protected void btnClear_Click(object sender, EventArgs e)
{
ClearControls();
}
private void ClearControls()
{
foreach (Control c in Page.Controls)
{
foreach (Control ctrl in c.Controls)
{
if (ctrl is TextBox)
{
((TextBox)ctrl).Text = string.Empty;
}
}
}
}
After you save your data with the click button you can set the textbox.text = ""
EDIT: After your comment that textbox.text = "" is not working....
When you hit the refresh it sounds like it is resubmitting your work again. Try just reloading the page by browsing to your page again.
Also be sure to check that you aren't saving your data on every postback but just on the button click event.
Do you have any code in your page load event?
Long shot here, but this worked in my case:
TextBox.Attributes.Remove("value");
This was on a textbox with the text mode sent to 'Password'.
just add this at the end of the event after page i going o be refresh
Response.Redirect("Registration.aspx");
here my WebForm name is "Registration.aspx" instead of this put your WebForm name.

Resources