How to refresh a web page in asp.net - 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;
}
}

Related

ASP.NET buttonclick called after master page load when trying to create session

I am trying to create a session when a user clicks a button. I want to update controls on the master.aspx page after the session is created from the login button being clicked. However the master.aspx page_load is being called before the button click function. Any ideas on how I could work around this?
Default.aspx.cs
protected void ButtonLoginIn_Click(object sender, EventArgs e)
{
Session["username"] = "Joe";
}
Master.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (Session["username"] == null)
{
LinkLoginInOut.Text = "Sign In";
}
else
{
string sessionId = Session["username"].ToString();
LabelDbUserName.Text = "Joe";
LinkLoginInOut.Text = "Log Out";
}
}
You can simply use FindControl on the Master
LinkButton lb = Master.FindControl("LinkButton1") as LinkButton;
lb.Text = "LinkButton Master";
You can try to move the code in the Page_Load event of the masterpage to an event, that is called after the button_click event. That way the changes to the session occur before the updating of the masterpage. You can try the Page_LoadComplete event. Please check the "asp.net page life cycle" documentation for the sequence, in which the different page events are called.

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 :-)

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;
}
}

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

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

Validation, Page events and ViewState

i have two buttons on the page. One button is responsible for text fields validation that are to do with registration and the other with loging in. The problem was when i press one of the buttons it refreshes the page and shows all the invalid fields (i dont want the registration fields to be checked by the RequiredFieldValidator whent the user presses the login button).
so what i did i used the initialization event.. to prevent this from happening...
static bool oneButtonPressed;
protected void Page_Init(object sender, EventArgs e)
{
if (oneButtonPressed)
{
REgisterAge.Visible = false;
RegisterAge2.Enabled = false;
RegisterAge3.Enabled = false;
RegisterPassword.Enabled = false;
RegisterPassword2.Enabled = false;
RegisterEmail.Enabled = false;
RegisterEmail2.Enabled = false;
}
else
{
EntryPasswordRequiredFieldValidator10.Enabled = false;
EntryNameEntryRequiredFieldValidator9.Enabled = false;
}
}
protected void entry_Click(object sender, EventArgs e)
{
oneButtonPressed = true;
}
protected void submitButton_Click(object sender, EventArgs e)
{
oneButtonPressed = false;
}
}
The probelm here is that the bool is always false when the page is posted back and loads again.. i do remember my teacher saying i could either use a ViewState or a static variable/method to preserve my values. Am i being wrong here.. do i have to use the ViewState?
Why don't you assign a validationgroup to each of the fields + the relevant submit button.
Different validation groups will ensure that validation won't fire on the irrelevant form.
<asp:TextBox runat="server" ID="txtName" ValidationGroup="vRegistration"></asp:TextBox>
<asp:LinkButton runat="server" ID="btnSubmit" ValidationGroup="vRegistration"></asp:LinkButton>

Resources