Can't get 'Text' property with asp-control - asp.net

How do I get properties (e.g. Text) with asp.net controls that were created programatically when page loading when IsPostBack parameter is true?
Schema:
creating control (e.g. TextBox box = new TextBox(); box.ID = "BoxID")
display control in page (e.g. SomeControlInPageID.Controls.Add(box))
user see this textbox (with id "BoxID", but we don't have a possibility to get text property use BoxID.Text, because it control was created programatically!) in page & puts in it some text
user click in button (asp:Button) in page and start page reloading process
start Page_Load method & IsPostBack parameter takes the true value
i try to use this code to get Text property in Page_Load method, but it's not work...:
void Page_Load()
{
if (Page.IsPostBack)
{
TextBox box = SomeControlInPageID.FindControl("BoxID") as TextBox;
string result = box.Text;
}
else
{
// creating controls programatically and display them in page
...
}
}
box.Text in this code always takes null value.

The key here is you need to make sure you recreate the dynamic controls each time the page is loaded. Once the controls are created, ASP.NET will be able to fill the posted back values into those controls. I've included a full working example below. Notice I add the control in the OnInit event (which will fire before Page_Load), and then I can read the value back out in the Page_Load event if a postback has occurred.
<%# Page Language="C#" AutoEventWireup="true" %>
<html>
<body>
<form id="form1" runat="server">
<asp:Panel ID="myPanel" runat="server" />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
<br />
Text is: <asp:Literal ID="litText" runat="server" />
</form>
</body>
</html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e)
{
if(Page.IsPostBack)
{
var myTextbox = myPanel.FindControl("myTextbox") as TextBox;
litText.Text = myTextbox == null ? "(null)" : myTextbox.Text;
}
}
protected override void OnInit(EventArgs e)
{
AddDynamicControl();
base.OnInit(e);
}
private void AddDynamicControl()
{
var myTextbox = new TextBox();
myTextbox.ID = "myTextbox";
myPanel.Controls.Add(myTextbox);
}
</script>

Please have a look into pageLifeCycle of an aspx page. You'll have to add the textbox within the Page_Init handler. Afterwards you may access your textBox in page_load event.
protected void Page_Init(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.ID = "textbox1";
tb.AutoPostBack = true;
form1.Controls.Add(tb);
}
protected void Page_Load(object sender, EventArgs e)
{
/// in case there are no other elements on your page
TextBox tb = (TextBox)form1.Controls[1];
/// or you iterate through all Controls and search for a textbox with the ID 'textbox1'
if (Page.IsPostBack)
{
Debug.WriteLine(tb.Text); /// only for test purpose (System.Diagnostics needed)
}
}
hth

Related

How to hide a control inside user control after click gridview button

This is the problem that bothers me for the past few days
I have a page with couple user controls.
On that page, there is a grid. If a user click on a particular row, a control (li) on the user control is shown or hidden based on the condition.
What makes things complicate is the user control and grid grid are all inside the update panel, but another user control is outside updaet panel
In the rowcommand event, I assigned a Session variable, dontshow. Based on the criteria, dontshow variable will be assigned to true or false. Then the control on the user control will be shown or hidden based on dontshow condition
So far, I am encountering the problem.
let's say (li) control was shown, but after the user click gridview, dontshow is set to true. (li) control should be hidden. No it does not. I have to click other button to do postback to make it happen
The same is true for the usercontrol outside the update panel. The logic is similar
Over the past few days, I have found out that I can't set visible property of controls in user control at page_load because that happens after rowcommand event. I moved the code to pre_render event
I even tried to use different control such as placeholder, panel, etc. That still does not work
I also trace the code. The pre_render event comes after row command. dontshow variable is false. The (li) control visible is set to false. However, after rendering, I can still see the control
What should I do?
Below are the snapshot of the codes
Thank you for the help
main.aspx
.....
<%# Register Src="../usercontrol1.ascx" TagPrefix="uctest1" TagName="ctrl1" %>
<%# Register Src="../usercontrol2.ascx" TagPrefix="uctest2" TagName="ctrl2" %>
...
<ctrl1:uctest1runat="server" ID="test1" />
<asp:UpdatePanel ID="updatepantest1" runat="server" UpdateMode="Always">
<ContentTemplate>
<uctest2:ctrl2runat="server" UpdateMode="Conditional" ID="test2" />
....
<asp:GridView ...>
//grid where rowcommand was executed
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
main.aspx code behind
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "something")
{
.....
if (x = 1)
session["dontshow"] = true
else
session["dontshow"] = false
}
}
user control aspx (usercontrol1)
....
<ul>
<li id="li1" runat="server">
<asp:label>this is test</asp:label>
</li>
<li >
<asp:label>this is test</asp:label>
</li>
<li >
<asp:label>this is test</asp:label>
</li>
</ul>
....
user control code behind (usercontrol1)
protected void Page_PreRender(object sender, EventArgs e)
{
bool dontshow = false;
if (Session["dontshow"] != null)
{
dontshow = (bool)Session["dontshow"]
}
if (dontshow)
li1.visible = false
else
li1.visible = true
}
user control aspx (usercontrol2)
....
<asp:PlaceHolder ID="placeholder1" runat="server">
<asp:label ID="label1" runat="server"></asp:label>
</asp:PlaceHolder>
....
user control code behind (usercontrol2)
protected void Page_PreRender(object sender, EventArgs e)
{
bool dontshow = false;
if (Session["dontshow"] != null)
{
dontshow = (bool)Session["dontshow"]
}
if (dontshow)
{
label1.visible =false;
}
else
{
label1.visible =true;
label1.Text ="this is test";
}
}
You could do it in your Row command like this
protected void gridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "something")
{
.....
var viewRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
HtmlGenericControl li1 = (HtmlGenericControl)viewRow.FindControl("li1");
if (x = 1)
{
session["dontshow"] = true;
li1.Visible = true;
}
else
{
session["dontshow"] = false;
li1.Visible = false;
}
}
}
Or, if you want to hide the control during data bound you could do it like this
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
HtmlGenericControl li1 = (HtmlGenericControl)e.Row.FindControl("li1");
if (dontshow)
li1.visible = false;
else
li1.visible = true;
}
}
Hope this helps :-)

Set value to TextBox in UserControl inside placeholder

I have an textbox inside an user control. I created dinamically this user control and load in placeholder.
But when I tried to assign a value to the textbox, I raised next below error:
Object reference not set to an instance of an object
This is the user control:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="IVT_FormClient.ascx.cs" Inherits="Evi.Sc.Web.Evi.IVT.Sublayouts.IVT_FormClient" %>
<asp:Panel ID="pnlContainer" runat="server">
<asp:TextBox ID="txtClientNumber" runat="server"></asp:TextBox>
</asp:Panel>
The access modifier is (In the user control):
public string TxtFirstName
{
get { return txtFirstName.Text; }
set { txtFirstName.Text = value; }
}
In the web form I have the control reference:
<%# Reference Control="~/Evi/IVT/Sublayouts/IVT_FormClient.ascx" %>
In the code behind of the user control is:
public partial class frm_VerifyIdentity : System.Web.UI.Page
{
IVT_FormClient ivtFormClient = new IVT_FormClient();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IVT_FormClient ivtFormClient = (IVT_FormClient)LoadControl("~/Evi/IVT/Sublayouts/IVT_FormClient.ascx");
Client UserClient = new Client();
UserClient = Load_ClientVerification(Server.HtmlEncode(Request.QueryString["ID"]).Trim());
if (UserClient != null)
{
ivtFormClient.TxtFirstName = UserClient.FirstName;
plhFormClient.Controls.Add(ivtFormClient);
}
}
}
}
The error occur is this line of code:
ivtFormClient.TxtFirstName = UserClient.FirstName;
Do not create an instance of a UserControl via constructor but with LoadControl as you have already done in Page_Load. However, you are doing that only if(!IsPostBack). Hence the control is instantiated the next postback via constructor.
Also, you have to recreate dynamic controls on every postback. I would suggest to add the UserControl delaratively to the page. You can hide/show it accordingly. Otherwise you need to create/add it always, best in Page_Init instead of Page_Load.
So this is not best-practise(just add it to the page) but should work as desired:
IVT_FormClient ivtFormClient = null;
protected void Page_Init(object sender, EventArgs e)
{
ivtFormClient =(IVT_FormClient)LoadControl("~/Evi/IVT/Sublayouts/IVT_FormClient.ascx");
Client UserClient = new Client();
UserClient = Load_ClientVerification(Server.HtmlEncode(Request.QueryString["ID"]).Trim());
if (UserClient != null)
{
ivtFormClient.TxtFirstName = UserClient.FirstName;
plhFormClient.Controls.Add(ivtFormClient);
}
}

How to refresh a web page in asp.net

In my code i added dynamically some text-boxes and labels.
Then by dropdownlist event i want to refresh the page in a way that all the added text-boxes and labels will remove from aspx
how can it be done?
I assume you're using C#.
You can redirect to the current page by doing this from your code-behind:
Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri);
Add AutoPostBack="True" for the DropDownList
Add an event to DropDownList for SelectedIndexChange
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
In the Code-Behind file :
private void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedIndex == 0)
{
Label1.Visible = false;
}
else
{
Label2.Visible = false;
}
}

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

Handling events from dynamic controls NOT added on Page_Load?

I've read a few articles on here and the web that have informed me that I cannot simply add a new control dynamically to the page, wire it to a handler, and expect it to work.
The solution given each time is that the dynamic controls need to be added to the page on Init each time.
My problem is, my controls are NOT added to the page on init, they are added after ANOTHER postback.
the workflow is this:
Page Loads
User fills in a textbox, clicks a button
Page Posts back, creating dynamic link controls in the button_click event based on the input
User clicks one of those link controls to proceed to the next step.
so if this is the behavior I need to support, is there any way to do this? It has to happen in the button_click of step 2, because the dynamic controls are based on the input the user puts in step 2.
have I painted myself into a corner here? how else could I handle such a workflow?
After you dynamically create a link button, set a flag in your page's view state. On postback, re-create the link button if the flag is set in view state. Here's a demo:
Markup:
<asp:Button runat="server" ID="button1" OnClick="button_Click" Text="Create button A" CommandArgument="A" />
<asp:Button runat="server" ID="button2" OnClick="button_Click" Text="Create button B" CommandArgument="B" />
<asp:PlaceHolder runat="server" ID="placeHolder"></asp:PlaceHolder>
Code-behind:
public partial class Default : System.Web.UI.Page
{
private bool LinkButtonCreated
{
get { return ((bool?)this.ViewState["LinkButtonCreated"]).GetValueOrDefault(); }
set { this.ViewState["LinkButtonCreated"] = value; }
}
private string LinkButtonCommandArgument
{
get { return (string)this.ViewState["LinkButtonCommandArgument"]; }
set { this.ViewState["LinkButtonCommandArgument"] = value; }
}
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
if (this.LinkButtonCreated)
this.CreateLinkButton(this.LinkButtonCommandArgument);
}
protected void button_Click(object sender, EventArgs e)
{
if (!this.LinkButtonCreated)
{
string commandArgument = ((Button)sender).CommandArgument;
this.LinkButtonCreated = true;
this.LinkButtonCommandArgument = commandArgument;
this.CreateLinkButton(commandArgument);
}
}
private void CreateLinkButton(string commandArgument)
{
LinkButton linkButton =
new LinkButton
{
ID = "linkButton",
Text = "Click me",
CommandArgument = commandArgument,
};
linkButton.Click += this.linkButton_Click;
this.placeHolder.Controls.Add(linkButton);
}
private void linkButton_Click(object sender, EventArgs e)
{
LinkButton linkButton = (LinkButton)sender;
linkButton.Text = "I was clicked! Argument: " + linkButton.CommandArgument;
}
}

Resources