Not getting user input from TextBox - asp.net

I'm starting to loose my nerves on some completely banal thing: I don't get user input from a TextBox :S
I do something like this (code behind aspx):
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
}
this._presenter.OnViewLoaded();
txtBox1.Text = "blah";
}
protected void Button1_Click(object sender, EventArgs e)
{
//Do sth with txtBox1.Text but when I read it, it is still the same as when a loaded the page at Page_Load, So if I entered "blahblah" in the txtBox1 via browser the text I get when I debug or run is still "blah"
}
And the aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="InsertStudent.aspx.cs" Inherits="IzPT.Vb.Views.InsertStudent"
Title="VnosProfesorja" MasterPageFile="~/Shared/DefaultMaster.master" %>
<asp:Content ID="content" ContentPlaceHolderID="DefaultContent" Runat="Server">
<h1>Student</h1>
<p>
<table style="width:100%;">
<tr>
<td style="width: 139px">
Name</td>
<td>
<asp:TextBox ID="txtBox1" runat="server"></asp:TextBox>
</td>
</tr>
</table>
</p>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
</p>
</asp:Content>
I also tried to do this with DetailsView and bind it to a list but when I read the values in the edit mode i had the same problem.
Any ideas?

You're setting the textbox Text property to "blah" on every Page_Load. Since ViewState has already been loaded at this point, you're overwriting whatever value the user entered.
If you only want to set the Text value one time, then make sure that you put it inside the if (!IsPostBack) check.
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._presenter.OnViewInitialized();
txtBox1.Text = "blah";
}
this._presenter.OnViewLoaded();
}

Your problem is that you are changing Value in Page_Load!
Page_Load is run before Button1_Click.
Move code from Page_Load to this
protected override void OnLoadComplete(EventArgs e)
{
txtBox1.Text = "blah";
}
Or protect your code... like this
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
}

Page_Load is being called during the post back which is resetting the value in your text box. Change to
if (!this.IsPostBack)
{
txtBox1.Text = "blah";
this._presenter.OnViewInitialized();
}

Personally i would have a property in the view to set the textbox value from the presenter. In OnViewInitialized() or OnViewLoaded().

Related

Why is the image button id not recognize

Why can't i call ImageButton.ImageUrl??
Default.aspx:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="Test1.jpg" CommandName="Delete" OnCommand="ImageButton_Command" />
</asp:Content>
Default.aspx.cs:
void ImageButton_Command(object sender, CommandEventArgs e)
{
string name = ***ImageButton1.ImageUrl***; //(Error)
}
There is no reason from the code provided for this control not to be recognised; it could be a discrepancy between your .aspx and .aspx.cs files (does the former properly relate to the latter?), or it could just be a build problem, have you done a complete clean and rebuild?
A solution, regardless of why this is happening for your current situation could be this:
protected void ImageButton_Command(object sender, CommandEventArgs e) {
var button = sender as ImageButton;
if (button != null) {
string name = button.ImageUrl;
}
}
It's important to do the null check here, since technically this event handler could be called by something other than the button(s) subscribed to raise the event, even though this shouldn't be the case.

Why is my repeater control empty on postback?

I think this is a "doh" moment caused by me not having dome WebForms dev for a few years..
I have a repeater which which contains a bunch of checkboxes:
<asp:Repeater EnableViewState="true" ID="IDTypesRepeater" runat="server" OnItemDataBound="IdTypesRepeaterItemDataBound">
<HeaderTemplate/>
<ItemTemplate>
<asp:CheckBox EnableViewState="true" ID="chkIdType" Text="<%# ((KeyValuePair<string,int>)Container.DataItem).Key %>" runat="server" />
<asp:HiddenField ID="idType" Value="<%# ((KeyValuePair<string,int>)Container.DataItem).Value %>" runat="server"/>
<br />
</ItemTemplate>
</asp:Repeater>
I need to get the checkboxes that are selected in the code behind:
foreach (RepeaterItem repeaterItem in IDTypesRepeater.Items)
{
if ( ((CheckBox)repeaterItem.FindControl("chkIdType")).Checked )
{
// Do something
}
}
But on postback, this code isn't working! I know about always databinding a repeater, so I've done this:
protected void Page_Load(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
So this repopulates the repeater, but the Update code never finds any checked checkboxes.. Any ideas?
Bind in the Page_Init event
protected void Page_Init(object sender, EventArgs e)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
Be sure to use the !Page.IsPostBack method in your pageload.
Otherwise, the Repeater will keep getting reset, and all your checkboxes
will be in there default value (unchecked)
This should fix it. You are binding the control on postback hence losing the values. You can bind it after handling any event to show the updated record.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
IDTypesRepeater.DataSource = DocTemplateHelper.GetApplicableIDTypes().Where(type => type.Value != 0);
IDTypesRepeater.DataBind();
}
}

Viewstate lost on postback for .net Sitecore page

I'm getting some strange behaviour with viewstate being lost on postback for a .net application using Sitecore. I'm assuming it might be some config variable somewhere but I'm new to Sitecore and don't really know where to start looking.
UPDATE: Sitecore has now gotten back to us with an answer. We had recently added the dtSearch module, and AutomaticDataBind was set to true in the dtSearch.config which overrides the setting in the web config. We've now removed it and it works fine again.
I've made a mini test if that might help. It's two usercontrols on one page, both with a repeater. When updating the viewstate gets lost so even if I'm binding the updated repeater again the data for the other one will be lost.
Usercontrol 1:
<asp:Repeater runat="server" ID="Repeater1" OnItemDataBound="Repeater1_ItemBind">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal1"></asp:Literal>
</li>
</ItemTemplate>
</asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> myTestList1 = new List<string>();
myTestList1.Add("a");
myTestList1.Add("b");
Repeater1.DataSource = myTestList1;
Repeater1.DataBind();
}
}
protected void Repeater1_ItemBind(object sender, RepeaterItemEventArgs e)
{
Literal Literal1 = (Literal)e.Item.FindControl("Literal1");
Literal1.Text = (string)e.Item.DataItem;
}
Usercontrol 2:
<asp:Repeater runat="server" ID="Repeater2" OnItemDataBound="Repeater2_ItemBind" OnItemCommand="Repeater2_Command">
<ItemTemplate>
<li>
<asp:Literal runat="server" ID="Literal2"></asp:Literal>
<asp:LinkButton ID="Update" CommandName="Update" runat="server">
update
</asp:LinkButton>
</li>
</ItemTemplate>
</asp:Repeater>
private string test = String.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
test = "a";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
protected void Repeater2_ItemBind(object sender, RepeaterItemEventArgs e)
{
char c = (char)e.Item.DataItem;
Literal Literal2 = (Literal)e.Item.FindControl("Literal2");
Literal2.Text = c.ToString();
}
protected void Repeater2_Command(object sender, RepeaterCommandEventArgs e)
{
if (e.CommandName == "Update")
{
test = "b";
Repeater2.DataSource = test;
Repeater2.DataBind();
}
}
Does anyone have any ideas what might be going on? Let me know if I need to provide any more information. The most annoying thing is that it was working last week but I have no idea what has changed!
Thanks,
Annelie
Do you have System.Web.UI.WebControls.Repeater in the "typesThatShouldNotBeExpanded" section of your web.config?
I've found that there are definitely some things that don't work with the regular PostBack model in Sitecore... but this Repeater should be OK.
One trouble area is having FieldRenderers inside Repeaters. They don't seem to restore the Item property correctly on Postback.
As far as I can see, this thread describes exactly the same problem, but with DataGrid. See if setting AutomaticDataBind to 'false' in web.config helps.

Template variable in ASP.NET

I have to add a Page's variable into a ItemTemplate but dont know how.
For example:
<rad:RadMenuItem ID="f" runat="server" Text="Products">
<ItemTemplate>
<div class="pitem"><%= MyText %></div>
</ItemTemplate>
</rad:RadMenuItem>
The MyText variable does exist in the context and has value but the text does not show up
Another question:
How can I add MyText to the ASP.NET page like the following?
<asp:Button Text="<%=MyText%>" .../>
I dont want to edit the code like btn.Text=MyText, just want to do that on the .aspx file as required.
Answers to second question. (Btw, you should only ask one question at a time here on Stack Overflow.)
You could use
<asp:Button Text="<%# MyText %>" />
if you call DataBind() in you code behind.
public void Page_Init(object sender, EventArgs e)
{
DataBind();
}
If the databind is expensive, I believe you could use this code, so that you only call it once, and then save the values in the ViewState.
public void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}

Loop in code block on click argument

Basically what i am trying to do is display a list of categories. And if the admin is logged in
i want to show some buttons next to each category. For example a button to delete it. The problem is that i dont know how to pass a parameter to the function that does the action.
Like i specify that on button click the function 'DeleteCat' must be called but if i cant pass the ID of the category to be deleted this wont work.
I know this can be done with commands and a repeater, but its not an option, i cant use a repeater.
So apparanly this is what i am aiming for:
But of course it does not work.
<%For Each Cat In Category.Children%>
<p class="SubCategory">
<%=Cat.Name%>
<%If User.Identity.Name = "Admin" Then%>
<asp:LinkButton ID="LinkButton6" runat="server" OnClick="AddItem" Text="A+" CommandArgument=<%=Cat.ID %> />
<%End If%>
</p>
<%Next %>
Your event handler AddItem should be able to evaluate the sender of the event and the CommandEventArgs
void AddItem(Object sender, EventArgs e)
{
int result;
bool returnValue;
Button clickedButton = (Button)sender;
returnValue = Int32.TryParse(clickedButton.CommandArgument, result);
// then other stuff happens ...
}
See detailed example here.
edit
Completed code sample as suggested by Brandon.
Alternative solution, using a CheckBoxList
...
<script language="C#" runat="server">
void Page_Load(Object Sender, EventArgs e)
{
if (!IsPostBack)
{
// bind data to controls
this.itemRepeater.DataSource = Category.Children;
this.adminList.DataSource = Category.Children;
this.itemRepeater.DataBind();
this.adminList.DataBind();
}
// set visibility according to user
this.itemRepeater.Visible = (User.Identity.Name != "Admin");
this.adminList.Visible = (User.Identity.Name == "Admin");
this.adminButton.Visible = (User.Identity.Name == "Admin");
}
protected void AddItem(object sender, EventArgs e)
{
foreach(ListItem currentitem in this.adminList.Items)
{
if(currentitem.Selected)
{
// do something with the selected value
int i;
bool isparsed = Int32.TryParse(currentitem.value, i);
}
}
}
</script>
</head>
<body>
<form id="mainForm" runat="server">
<asp:Repeater ID="itemRepeater" runat="server">
<ItemTemplate>
<p><%# DataBinder.Eval(Container.DataItem, "value") %></p>
</ItemTemplate>
</asp:Repeater>
<asp:CheckBoxList ID="adminList" runat="server" />
<br />
<asp:Button ID="adminButton" OnClick="AddItem" runat="server" Text="Add seleted Items" />
</form>
</body>

Resources