Event before redirect to another page - c#

I've just developed a new GridView MultiFilter control ( CompositeControl ) that works like the image below:
I use ViewState for my control's properties so it keeps all values after postback. I want to save my control properties to a Session before redirect so I can load properties back to my control when my page loads again.
Does anyone have any suggestions about how this can be accomplished?

You have to do 2 things in this list page:
(1)Page load
(2)Search click
And 1 thing in detail (redirected) page:
(3)Pass some query string while back to list page
(1)while page load decide to load normal or searched (back from detail page) data
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Request.QueryString["back"] != null)
bindDataFirst();
// same data load logic as present
else
bindDataForBack();
// you come back from detail page hence bind search & grid data
}
private void bindDataForBack()
{
strName = Session["SearchName"] == null ? "" : Session["SearchName"].ToString();
// check session search conditions & data and bind
//also bind grid by respective search parameters & search options (top side)
(2)search click store search data into session
public void btnSearch_Click(object sender, System.EventArgs e)
{
Session["SearchName"] = strName;// store search data into session variables
//bind grid by respective search parameters
(3)in redirected (detail) page set back button like:
public void btnBack_Click(object sender, System.EventArgs e)
{
Response.Redirect("ListPage.aspx?back=1");
I hope this may helps you. I suggest to implement this with one textbox & grid and then try with your present scenario.
Please mark this answer useful if this solve your problem.

You can assign data to Session the pretty much the same way you assign to ViewState.
It's a key value dictionary just like ViewState.
Ex:
Session["someKey"] = "An arbitrary value";

If you can redirect to another page using form submit then You can post your form to the required page using action attribute of your page. This way values of all controls will be available in the Request["KEY NAME HERE"]
<form action="anotherpage.aspx" id="frmData">
<!-- YOUR CONTROLS HERE -->
<input type="submit" value="Submit" />
</fomr>
you can also submit your form using JS
$("#frmData").submit();

Related

Keep ASP.NET Multiview in current ActiveViewIndex

In my ASP.NET Web Forms, I have a MultiView that, when it is on ActiveViewIndex 2, will always go back to ActiveViewIndex 0 when a postback is done. When I insert/update a row in the database, I need it to show on the current ActiveViewIndex after it submits to the database. How do I keep it on the current ActiveViewIndex after the web form submits? Maybe this can be done using ViewState but I have never used it before so I wouldn't know how.
For storing per-page data you can use ViewState, or have hidden input field which will preserve data after each post-back.
Viewstate is like a dictionary: when view 2 is displayed store that to viewstate (ViewState["activeTab"] = 2;), and on page load check if there's some value in viewstate, and change active view index if it is:
protected void Page_Load(object sender, EventArgs e)
{
if (ViewState["activeIndex"] != null)
multiView.ActiveViewIndex = (int) ViewState["activeIndex"];
}

Enter data in web form, output to a new page

I am using a simple <form> to collect data from a user. The user clicks a simple button to enter the data: <input type="submit" name="cmd" value="OK">. Currently the application does a simple post back, displays the filled in form, and under the form, displays the results.
The users now want the results to go to another page. Basically they want to change a variable and compare the results in different tabs. My first proposal was to keep the post back and then add a hyperlink using target="_blank" to push the results to a different tab but they do not like the two-clicks: OK button then hyperlink.
Is it possible to send the results of a form input to another page?
I am programming in C# using ASP.NET.
you can do this by postbackurl property in c#. thus helps you to access your previous page controls and there output on next page. you can also do this by using hidden field and post or get method. both options are good and reliable.
reference
Seeing that you are using ASP.Net, I would recommend you utilise the power of the code-behind process. One option that you can do in addition to the above responses is use a QueryString in your URL as you do a re-direct if that is available to you as a requirement. Example 1. Use a ASP Button
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
string url = "NextPageViewer.aspx?result=" + resultText;
//You can use JavaScript to perform the re-direct
string cmd = "window.open('" + url + "', '_blank', 'height=500,width=900);";
ScriptManager.RegisterStartupScript(this, this.GetType(), "newWindow", cmd, true);
//or you can use this - which ever you choose
Response.Redirect(url);
}
///On the next page - the one you have been redirected to, perform the following
protected void Page_Load(object sender, EventArgs e)
{
//extract the query string and use it as you please
int result = Convert.ToInt32(Request.QueryString["resultText"]);
}
Example 2. Use a Session variable and store your dataset/result in an user defined object or DTO - this is just a class with a bunch of setters and getters
Perform the click event on the ASP button except this time you would do something like this:
protected void btnOriginalPage_Click(object sender, EventArgs e)
{
ObjectWithInfo.Result = result;
Session["friendlyName"] = ObjectWithInfo;
Response.Redirect("NextPageViewer.aspx");
}
//On the next page - the one you have been redirected to, perform the following
//The good thing with Session variables is that you can access the session almost anywhere in you application allowing your users to perform the comparison they require.
protected void Page_Load(object sender, EventArgs e)
{
//extract the data from the object to use
int result = ((ObjectWithInfo)(Session["friendlyName"])).Result;
}

ASP.NET : my html element repeated after my postback finish

i have an asp web page and my page contains to RadCombobox. for first drop down i set item_requested method (when the user click on it and select the theater name.it has a post back and after post back the second drop down fill with sans numbers ).
my problem is here:
after page post back the second field set duplicate in my page. in these pictures you can see that.
Before post back:
the second image :(After postback the second fielset repeated)
Note:
all of my controls in update panel. after i remove update panel from my page i doesn't happen again.Do you know the reason ? :[
You probably have some logic where you are adding this component. Try to check IsPostBack before adding the component to the page. Just like that:
if (!IsPostBack)
{
//dynamically add component
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
// ASPX C#
}
}
ASP
................... IM NOT GOOD AT ASP

Activating a ASP Multiview control's view from other page without querystring/session

I was looking for an alternative that may be used to activate a multiview control of other page without passing a querystring/session variable.
Basically, my Home.aspx page has a link that takes us to a specific page say "NewPage.aspx". The NewPage.aspx page has a multiview control that has three child views.
I want to click on the link of the Home.aspx and go to NewPage.aspx with MultiView1.ActiveViewIndex=1. Please remember that I do not want to pass any querystring variable as that link already contains some encrypted data as querystring and adding another variable can cause the data to corrupt. Maintaining a session isn't a solution as well because the application is quite big.
Any inbuilt method that can activate that view? (I don't seem to be talking practical but any help is really appreciated)
If you are asking about how navigate to this page I would wire up a button event (I prefer to do so in OnInit). Something like this:
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.btnlinkclick.Click += new EventHandler(btnlinkclick_Click);
}
void btnlinkclick_Click(object sender, EventArgs e)
{
this.MultiView1.ActiveViewIndex = 1;
}
This should work for you.

How to use a form with GET to utilize querystring, but not have VIEWSTATE in querystring?

I'm trying to create a C# webpage that uses a GET method so I can bookmark form data, email it to other people, etc., but changing the form method to GET results in the querystring containing the VIEWSTATE. I can take out the runat=server tags (as mentioned here) but then I don't know how to set the values of my form fields. I don't mind manually persisting them, but I can't figure out how.
So, how do I either utilize the viewstate but keep it out of my querystring, or access the form controls without runat=server?
If you are using, for example, a search/filter form on your webpage that you want via GET, here's a pattern I've used multiple times in my own code. When the user submits a button to process what they've entered in the form, I let it hit my code-behind as a POST as normal. Then I manually construct a GET request and redirect to it:
protected void BtnSubmit_Click(object sender, EventArgs e)
{
var query =
"?filter1=" + TxtFilter1.Text +
"&filter2=" + TxtFilter2.Text +
"&filter3=" + TxtFilter3.Text; // etc.
Response.Redirect(query);
}
Then on load of the page, I process the entries in the query string to pre-populate the relevant form controls on the page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TxtFilter1.Text = Request.QueryString["filter1"] ?? "";
TxtFilter2.Text = Request.QueryString["filter2"] ?? "";
TxtFilter3.Text = Request.QueryString["filter3"] ?? "";
// etc.
}
}
This allows me to have control over what actually goes into the QueryString without messing with the built-in form POST managed by ASP.Net (including View State). Also, when a user has filled out the form and pressed the button, they have a linkable url, and even though it was a POST, if they refresh the page they don't get that annoying message about re-sending data to the server.

Categories