ASP.NET C# use session data from dropdown - c#

OK so i am a bit lost and could do with some help.
I am creating a program that a user inputs data into a form on the default page (I have that working).
Then i use session variables to get the data input from a text box on the default page and place that data into a drop down menu on Page2 (I have that working).
What im trying to do now is use the data selected from the drop down on the page2 and output it on to a label. any help would be appreciated.
Page2 code bellow session that populates drop down
public partial class About : Page
{
protected void Page_Load(object sender, EventArgs e)
{
MyFruit = Session["Fruitname"] as List<string>;
//Create new, if null
if (MyFruit == null)
MyFruit = new List<string>();
DropDownList1.DataSource = MyFruit;
DropDownList1.DataBind();
}

You can use SelectedIndexChanged event of DropDownList to handle this.
your AutoPostBack property of DropDownBox should be set to True
sample code as below:
Design code: page.aspx
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>name1</asp:ListItem>
<asp:ListItem>name2</asp:ListItem>
</asp:DropDownList>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
CodeBehind File: page.aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedValue.ToString();
}

Not sure if this is what you are looking for but what I am guessing is you want an event for your drop down list to get the info and place it into a session to pass onto the next page
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string item=DropDownList.SelectedItem;
Session["selectedItem"]=item;
Response.Redirect("TheNextPageURL")
}
public partial class TheNextPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["selectedItem"]!=null)
{
Label1.Text=Session["selectedItem"].toString();
}
}
}
Hope that helps

Related

ASP.NET C# why cant I change the text in a textbox in a webpage

I have an ASP.NET project with a simple webpage.When I load the page the textbox display "Hello". When I click the "btnUpload" the text goes away.
I tried !IsPostBack in the Load_Form function gut the just make the text stay the same.
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello";
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
This is because you have not used
if(!IsPostBack) { }
inside Page_load() method
so modify your code as:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack) {
TextBox1.Text = "Hello";
}
}
protected void btnUpload_Click(object sender, EventArgs e)
{
TextBox1.Text = "Good Bye";
}
Because, you have not used this, thats why, on the postback of button it changes the value to TextBox1.Text = "Good Bye"; but it follows completing the postback with Page_Load() so it changes it again to TextBox1.Text = "Hello"; that's the reason default value of Page_Load() is shown after render.
You have to use the following condition in the Page_Load event handler, not in the Load_Form:
if (!IsPostBack) {
TextBox1.Text = "Hello";
}
Your button click event handler is ok.
Hope it helps!
Please see below code :
Default.aspx:
<asp:TextBox ID="text1" runat="server"></asp:TextBox>
<asp:Button ID="btn" runat="server" OnClick="btn_Click" />
Make sure aspx page contain same code for button as shown above.

Get Set values of TextBox Control from server side

Let say I have this code on Page load of ASP.NET Webform
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "123";
}
and this is my control in aspx file
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
If I change the textbox data from 123 to 12548 or any thing in the front end and then I click the button
Now in this is my code behind button click event
protected void Button1_Click(object sender, EventArgs e)
{
string s = TextBox1.Text;
}
Now in TextBox1.Text I should be getting 12548 or updated value instead I am getting 123 which I have already set in page load.
Now I want to get the updated value, how may I do it the right way.
Wrap it in a NOT is Postback
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Text = "123";
}
}
or remove it completely:
protected void Page_Load(object sender, EventArgs e)
{
//not here
}
<asp:TextBox ID="TextBox1" Text="123" runat="server"></asp:TextBox>
Modify the Page_Load as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
TextBox1.Text = "123";
}
}
The problem was this "In ASP.Net every time you cause a postback of any kind, including handling events like button clicks, you're working with a brand new instance of your page class that must be rebuilt from scratch. Any work you've done previously to build the page on the server is gone. That means running the entire page life cycle, including your page load code, and not just the click code.
Every time you perform any event in the front end it will recreate the page and call the pageload method again and the page would be actually reset. To avoid it one should use the following code
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
//default code
}
}

GridView don't Show on GridView_PageIndexChanging

I have a panel ViewStock where i am viewing stock in a gridview from database and DataBind() it via code. Allowed paging and created and event "OnPageIndexChanging" in gridview tag in html, Implemented the defined code above and paging in an event as follows:
HTML:
<asp:Panel ID="Panel_StockView" runat="server">
<asp:GridView ID="GridView_Stock" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" runat="server"></asp:GridView>
</asp:Panel>
Code C#:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(cs))
{
//Sql command here
/sql adapter and filled datatable
sdaStockView.Fill(dtStockView);
GridView_Stock.DataSource = dtStockView;
GridView_Stock.DataBind();
}
}
And now the Implemented Paging
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.DataBind();
GridView_Stock.PageIndex = e.NewPageIndex;
}
it does work but partially. It does the paging and does the data correctly. But, the issue is when i click the page '2' the panel blanks out just like in the picture i uploaded See this Image, then i click the link button that redirects me to the panel again and opens the page '2' of the gridview with valid data.
How to resolve this issue?
Extract the logic which binds the GridView to data into a new method.You can call it BindData() for example:
private void BindData()
{
using (SqlConnection con = new SqlConnection(cs))
{
sdastockview.fill(dtstockview);
gridview_stock.datasource = dtstockview;
gridview_stock.databind();
}
}
Call this method inside LinkButton_Panel_ViewStock_Click to populate the GridView:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
this.BindData();
}
Lastly, call it again to re-populate the GridView during paging:
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.PageIndex = e.NewPageIndex;
this.BindData();
}
Just make those three little changes and it will work. I've tried this on my side and it's working just fine.
Save your DataSet somewhere like ViewState on LinkButton_Panel_ViewStock_Click after filling DataSet like this
ViewState["ds"] = dtStockView
In PageIndexChanging write like this
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
panel_ViewStock.visible = true;
GridView_Stock.PageIndex = e.NewPageIndex;
GridView_Stock.DataSource = ViewState["ds"] as DataSet
GridView_Stock.DataBind();
}
Hope this will help you
You can try to use:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)

onclick second click

New to asp.net, I am having a problem on a website I am creating, I am using a master page to build my pages. I am trying to change the css class of a li tag using the onclick event in linkbuttons:
<asp:LinkButton runat="server" id="AboutButton" OnClick="about_click" PostBackUrl="about.aspx"><span>About</span></asp:LinkButton>
This linkbutton calls a function in the master page's code behind:
protected void about_click(object sender, EventArgs e)
{
if(Page.IsPostBack)
{
about.Attributes.Add`enter code here`("class", "current");
}
}
This only works when the page is loaded and the button is clicked again. Any help would be greatly appreciated.
By adding: if(Page.IsPostBack) you're specifically telling it not to execute that code the first time the page is loaded, but you want it to happen when the page is first loaded, by the sounds of the question.
Why did you add if(Page.IsPostBack). Try this
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
about.Attributes.Add("class", "current"); //initial setting here
}
}
protected void about_click(object sender, EventArgs e)
{
about.Attributes.Add("class", "current");
}

Hiding Asp.Net user control using c#

I want to know if I could hide/show an asp user control when pressing a button in another user control on the same master page?
The Visible property will do this for you. The Click handler on your Button would be something like:
protected void ToggleButton_click(object sender, EventArgs e)
{
TargetControl.Visible = !TargetControl.Visible;
}
if you have got 2 users controls in same page . you can use this code :
in usercontrol 1
<asp:button Text="hide" runat="server" ID="B1" OnClick="HideOtherUserControl" />
and in code-behind
protected void HideOtherUserControl(object sender, EventArgs e)
{
Parent.FindControl("WebUserControl1").Visible = false;
}

Categories