redirection between two pages Asp.net - asp.net

I have a asp.net form with several text boxes and a link-button which is redirected to the other page. here is my form:
<table>
<tr>
<td >
<asp:TextBox ID="TextBox_Name" runat="server" Width="100%"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="TextBox_FatherName" runat="server" Width="100%"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="LinkButton_BirthPlace" runat="server" OnClick="LinkButton_BirthPlace_Click" >Search</asp:LinkButton> </td>
</tr>
and it is my method for link button :
protected void LinkButton_BirthPlace_Click(object sender, EventArgs e)
{
Response.Redirect("~/Profile/CitySearch.aspx");
}
my problem is: When I enter value in my text boxes and then click on link button , redirected to CitySearch.aspx page and when I come back to base page my values in text boxes were deleted.I need those values that I enterned.
What should I do?

You need to repopulate your textboxes with your information from the Session. Put a method similar to this on your "base page"
Psuedocode:
protected void Page_Load(object sender, EventArgs e)
{
string Birthplace = Session["Birthplace"].ToString();
MyBirthplaceTextBox.Text = Birthplace;
}
You'll need multiple Session values to store multiple pieces of information. You'll also want to code in some checks to make sure your session variables aren't null, and take action accordingly.

Try like This on your First Page:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (Session["search"]!=null)
{
TextBox_Name.Text = Session["search"].ToString();
}
}
}

Try like this,
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack==false)
{
if (Session["search"]!=null)
{
TextBox_Name.Text = Session["search"].ToString();
}
}
}
protected void LinkButton_BirthPlace_Click(object sender, EventArgs e)
{
Session["search"] = TextBox_Name.Text;
Response.Redirect("~/Profile/CitySearch.aspx");
}

Related

AutoPostBack= "true" not functioning properly with dropdown list asp.net

I have a dropdown list that is populated with values from a DB table, when you select a value from the list a repeater appears and shows extra details about that selected value. The only problem is that the page only refreshes for the first value selected, if you try to make a different selection the page doesn't change.
The repeater seems to be working fine, but there must be something wrong with the AutoPost back in the dropdown.
<asp:DropDownList ID="DropDownList1" Width="150px" runat="server" DataSourceID="SqlDataSource1" DataTextField="LCID" DataValueField="LCID" EnableViewState="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack= "true" >
</asp:DropDownList>
this is the code from the aspx.cs file:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("http://localhost:31003/?LCID="+ DropDownList1.SelectedValue);
}
}
If anyone can see what I am missing from my program I would be very grateful, thanks.
you are missing the PageName in Response.Redirect
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("~/PageName.aspx?LCID="+ DropDownList1.SelectedValue);
}

How to output current user's AD sAMAccountName to a textbox

I'm designing an eform that submits information into a CSV. I need to display the current user's sAMAccountName and the Datetime.Today in two of the text fields.
Any idea of how this would be done?
I've been trying: c#
public string UserName { get { return Session["UserName"]; } }
to:
<asp:TextBox ID="UserName" runat="server" Width="44px" ReadOnly="true"></asp:TextBox>
You can pre-populate input values in your form in the Page_Load event:
protected void Page_Load(object sender, EventArgs e) {
UserName.Text = "my value";
TxtCurrentDate.Text = DateTime.Today.ToString();
}
Just augmenting this quickly with another bit of info, I was originally adding the Page_Load cmds to the codebehind's Page_load event, be sure that all prepopulation of text-boxes is handled on the .aspx page.
All contained within the .aspx
protected void Page_Load(object sender, EventArgs e)
{
txtDate.Text = DateTime.Now.ToString();
}
<td><asp:TextBox ID="txtDate" runat="server" Width="181px" ReadOnly="true"></asp:TextBox></td>
Thanks again for the helpful reply mellamokb

Ajax CalendarExtender, How to get the date after i click on a button?

I want to add add an Ajax CalendarExtender to my page. And then after selecting a date and clicking on a button I get the selected Day in a label.
I have a text box which is the target of the CalendarExtender
<asp:TextBox ID="DateText" runat="server" ReadOnly="true" ></asp:TextBox>
<ajaxToolkit:CalendarExtender
ID="Calendar1"
runat="server"
TargetControlID="DateText"
Format="MMMM d, yyyy"
PopupPosition="Right"
/>
<asp:Button runat="server" ID="Button1" onclick="Button1_Click" />
In the Code Behind:
On the first page load I set the date to Today.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Calendar1.SelectedDate = DateTime.Today;
}
}
In the Button1_Click Event
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = Calendar1.SelectedDate.Value.Day.ToString();
}
The problem is when I click the button (or after any post backs) the value selected get reset to Today's date.
And if I don't set it to DateTime.Today in the Page_Load It get reset to null and throw a null exception.
How can I solve this?
Thank you very much for any help.
I hope i was clear
Maybe this will work:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if(!Calendar1.SelectedDate)
Calendar1.SelectedDate = DateTime.Today;
}
}
The solution to the issue is making use of Request.Form collections. As this collection has values of all fields that are posted back to the server and also it has the values that are set using client side scripts like JavaScript.
Thus we need to do a small change in the way we are fetching the value server side.
C#
protected void Submit(object sender, EventArgs e)
{
string date = Request.Form[txtDate.UniqueID];
}

button events not fire in asp.net

The if (!Page.IsPostBack) is always false went the page loads after i click a linkbutton and it never goes into the linkbutton event. Desperately need help! Googled as much as I can. I am kinda new to asp
This is the code that i have in server:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaultView();
}
}
private void SetDefaultView()
{
MultiView1.ActiveViewIndex = 0;
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 0;
}
protected void LinkButton2_Click(object sender, EventArgs e)
{
MultiView1.ActiveViewIndex = 1;
}
// and below..
<td>
<asp:LinkButton ID="LinkButton1" runat="server"
onclick="LinkButton1_Click">Tab1</asp:LinkButton>
</td>
<td>
<asp:LinkButton ID="LinkButton2" runat="server" onclick="LinkButton2_Click">Tab2</asp:LinkButton>
</td>
Is the code part of a dynamically loaded UserControl? If so, in what stage of the asp.net lifecycle are you adding the UserControl?
If that is not the case, how do you access the Page?

Paging ObjectDataSource

My ASP page code:
<asp:ObjectDataSource runat="server" ID="odsResults" OnSelecting="odsResults_Selecting" />
<tr><td>
<wssawc:SPGridViewPager ID="sgvpPagerTop" runat="server" GridViewId="sgvConversionResults" />
</td></tr>
<tr>
<td colspan="2" class="ms-vb">
<wssawc:SPGridView
runat="server"
ID="sgvConversionResults"
AutoGenerateColumns="false"
RowStyle-CssClass=""
AlternatingRowStyle-CssClass="ms-alternating"
/>
</td>
</tr>
Class code:
public partial class Convert : System.Web.UI.Page
{
...
private DataTable resultDataSource = new DataTable();
...
protected void Page_Init(object sender, EventArgs e)
{
...
resultDataSource.Columns.Add("Column1");
resultDataSource.Columns.Add("Column2");
resultDataSource.Columns.Add("Column3");
resultDataSource.Columns.Add("Column4");
...
odsResults.TypeName = GetType().AssemblyQualifiedName;
odsResults.SelectMethod = "SelectData";
odsResults.SelectCountMethod = "GetRecordCount";
odsResults.EnablePaging = true;
sgvConversionResults.DataSourceID = odsResults.ID;
ConversionResultsCreateColumns();
sgvConversionResults.AllowPaging = true;
...
}
protected void btnBTN_Click(object sender, EventArgs e)
{
// add rows into resultDataSource
}
public DataTable SelectData(DataTable ds,int startRowIndex,int maximumRows)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Columns.Add("Column4");
for (int i =startRowIndex; i<startRowIndex+10 ;i++)
{
if (i<ds.Rows.Count)
{
dt.Rows.Add(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString(),
ds.Rows[i][2].ToString(), ds.Rows[i][3].ToString());
}
}
return dt;
}
public int GetRecordCount(DataTable ds)
{
return ds.Rows.Count;
}
protected void odsResults_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["ds"] = resultDataSource;
}
}
On clicking the button resultDataSource receives some rows. Page reload and we can see result in sgvConversionResults. First 10 rows. But after click next page in pager we have message "There are no items to show in this view". When I try debug I find that after postBack page (on click next page) input params "ds" is blank, ds.Rows.Count = 0 and etc... As though resultDataSource became empty((
What did I do incorrectly?
onPostBack all variables get default values, sgvConversionResults save her structure but has clear rows. How I can save sgvConversionResults data onPostBack event???
Maybe try:
protected void Page_Load(object sender, EventArgs e)
{
sgvConversionResults.DataBind();
}
Your code seems a bit convoluted though, any particular reason you are using an ObjectDataSource? Cause you can bind the datatable directly to the gridview in the codebehind
I transmit resultDataSource with ViewState and this work success!

Resources