How to pass dropdownlist value to another page - c#

I have a DropDownList with button:
<form id="form1" runat="server">
<asp:DropDownList ID="DropDay" runat="server"/>
<input type="submit" value="Save" runat="server" id="btn"/>
</form>
C# code for the page:
protected void Page_Load(object sender, EventArgs e)
{
btn.ServerClick+=new EventHandler(btn_ServerClick);
}
void btn_ServerClick(object sender, EventArgs e)
{
HttpContext value= HttpContext.Current;
value.Items.Add("Day", DropDay.SelectedValue);
Server.Transfer("ASP_2.aspx");
}
In ASP_2 I have a label, that I need to set the selected Dropdownlist value. And here is the code for ASP_2.aspx:
protected void Page_Load(object sender, EventArgs e)
{
HttpContext value= HttpContext.Current;
lblDay.Text = value.Items["Day"].ToString();
}
The problem is, that the value always is the first item from the dropdown. How do I set the selected value?
EDIT
the way I am filling the dropdown list The code goes in Page_Load:
DropDay.Items.Clear();
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}

Your code for filling the DDL should be (at least) :
if (!IsPostBack)
{
DropDay.Items.Clear();
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}
}
Hope this will help

You can use this in first page:
Server.Transfer("ASP_2.aspx?dropdown="+DropDay.SelectedValue);
and in the second page
protected void Page_Load(object sender, EventArgs e)
{
string val=Request.QueryString["dropdown"].toString();
}

Maybe you left the autopostback property of the dropdownlist true.

Figured out it by myself, just needed to remove "DropDay.Items.Clear();"
//DropDay.Items.Clear(); removed this line and the thing was fixed
for (int i = 1; i <= 10; i++)
{
DropDay.Items.Add(i.ToString());
}

Have you tried DropDay.SelectedItem.Text?

Related

Timer keep reloading the page in c# [duplicate]

I want to increase values on timer tick event but it is not increasing don't know what I am forgetting it is showing only 1.
<asp:Timer ID="Timer1" runat="server" OnTick="Timer1_Tick" Interval="1000"></asp:Timer>
private int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
{
i++;
Label3.Text = i.ToString();
}
You can use ViewState to store and then read the value of i again.
int i = 0;
protected void Timer1_Tick(object sender, EventArgs e)
{
//check if the viewstate with the value exists
if (ViewState["timerValue"] != null)
{
//cast the viewstate back to an int
i = (int)ViewState["timerValue"];
}
i++;
Label3.Text = i.ToString();
//store the value in the viewstate
ViewState["timerValue"] = i;
}
Check whether the form is posted back and then assign values. Check IsPostBack
private int i;
protected void Timer1_Tick(object sender, EventArgs e)
{
if (!IsPostBack)
{
i = 0;
}
else
{
i = Int32.Parse(Label3.Text);
i++;
}
Label3.Text = i.ToString();
}
Generally speaking it is not a good practice to store values inside of views (such as asp.net page). It could be overwritten each time the request is sent.
You could store your data elsewhere:
public static class StaticDataStorage
{
public static int Counter = 0;
}
And use it:
protected void Timer1_Tick(object sender, EventArgs e)
{
StaticDataStorage.Counter++;
Label3.Text = StaticDataStorage.Counter.ToString();
}

How to enable button based on gridview value?

I have a column in SQL:
Status
open
Close
and Gridview with Boundfield value='Status'
When a user selects a row and the Status == open then it should display a button. Otherwise ist hiden.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string y = Data.Rows[GridView1.SelectedIndex][5].ToString();
if (y == "open")
{
btnAccept.Visible = true;
}
else
{
btnAccept.Visible = false;
}
}
<asp:Button Text="Accept" OnClick="btnAccept_Click" Visible="false" ID="btnAccept" runat="server" />
This could be the solution:
string y = GriView1.SelecteRows[0].Cells[5].Value.ToString();
Modify your code like this
string y = GridView1.SelectedRow.Cells[5].Text;
It's counter-intuitive, but GridView1.SelectedRow is not set until after SelectedIndexChanged completes.
Also visibility has nothing to do with the button being Enabled or Disabled.
Use the Enabled Property of the Button:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gv = (GridView) sender ;
btnAccept.Enabled = (gv.Rows[ gv.SelectedIndex ].Cells[5].Text == "open");
}
Also, if you haven't already, consider styling disabled buttons with CSS:
input[type=button][disabled],
button[disabled]
{
cursor:not-allowed;
}

connect asp repeaters with objects outside repeaters

I have a dropdown in my repeater.. i want to copy its value to textbox that is outside repeaters..
here is what I want to do
copy value from dropdown[1] to textbox1
copy value from dropdown[2] to textbox2 and so on...
here is my code
ASP:
<asp:DropDownList ID="fmFrom" runat="server" Height="20px" Width="120px" DataSourceID="BrDatasource" DataTextField="branchName" DataValueField="branchCode" AutoPostBack="true" onselectedindexchanged="fmFrom_SelectedIndexChanged"></asp:DropDownList>
c#:
protected void fmFrom_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < rateRepeater.Items.Count; i++)
{
DropDownList from = (DropDownList)rateRepeater.Items[i].FindControl("fmFrom");
TextBox1.Text = from.SelectedValue.ToString();
}
}
here all my textbox gets the value of the last dropdown only...
what should I do?
Try:
protected void fmFrom_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < rateRepeater.Items.Count; i++)
{
DropDownList from = (DropDownList)rateRepeater.Items[i].FindControl("fmFrom");
((TextBox)FindControl("TextBox" + (i + 1))).Text = from.SelectedValue.ToString();
}
}
change this:
TextBox1.Text = from.SelectedValue.ToString();
to:
TextBox1.Text += from.SelectedValue.ToString();

Get the value of Label in listview

I have a listview with invisible label to store the category Id.
what I want to do is to assign the text of the label to a cookie or session on button click.
the problem is my cookie is always null when I try to display the value outside the listview.
here is my aspx code:
<asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource" >
<EmptyDataTemplate>No DataFound</EmptyDataTemplate>
<ItemTemplate>
<div class="service" style="margin-bottom:10px;width:230px;">
<h4 style="font-family:Corbel;" ><%#Eval("CatName") %></h4>
<asp:Label runat="server" Visible="false" ID="lblcat"><%#Eval("CatId") %></asp:Label>
<asp:Button runat="server" ID="btnTest" Text="View Items" OnClick="btnTest_Click" />
</div>
</ItemTemplate>
</asp:ListView>
my C# code:
protected void btnTest_Click(object sender, EventArgs e)
{
Response.Cookies["cat"].Value = "test";
foreach (ListViewItem item in catListView.Items)
{
Label catLabel = (Label)item.FindControl("lblcat");
Response.Cookies["cat"].Value = catLabel.Text.ToString();
}
}
any help will be appreciated.
thx in advance
Sam
It sounds like your ListView might not be databound when your Button click event is called.
I would recommend that you use the ItemCommand event handler. It is most appropriate for this type of handling. Update your ListView declaration to handle that event:
<asp:ListView runat="server" ID="catListView" DataSourceID="CategoriesDataSource
OnItemCommand="catListView_ItemCommand" >
Note: Don't forget to remove the "OnClick" event handler from the Button
And then write your even handler code like this:
protected void catListView_ItemCommand(object sender, ListViewCommandEventArgs e)
{
Label catLabel = (Label)e.Item.FindControl("lblcat");
Session["currentCatLabelText"] = catLabel.Text;
}
This also has the advantage of not looping through all your ListView items, but just looking at the one you clicked.
Hope this helps :)
string[] catvals = new string[catListView.Items.Count];
for (int i =0; i< catListView.Items.Count; i++)
{
Label catLabel = (Label)catListView.FindControl("lblcat");
catvals[i] = catLabel.Text;
}
for(int i = 0 ; i < catvals.Length; i++)
{
Response.Cookies["cat"+ i.ToString()].Value = catvals[i].ToString();
}
Actually you are trying to add your cat in one key. I mean you are overwriting values in loop.
You should change your code in this way:
In your C# code:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
HttpCookie testCokkie = new HttpCookie("ExampleCookie");
testCokkie.Expires = DateTime.Now.AddDays(5);
Response.Cookies.Add(testCokkie);
}
}
protected void btnTest_Click(object sender, EventArgs e)
{
HttpCookie ExampleCookie = Request.Cookies["ExampleCookie"];
int tempIndex = 0;
foreach (ListViewItem item in catListView.Items)
{
Label catLabel = (Label)item.FindControl("lblcat");
ExampleCookie["cat" + tempIndex.ToString()] = catLabel.Text.ToString();
tempIndex = tempIndex + 1;
}
}
TO read this cookie
HttpCookie exampleCookie = Request.Cookies["ExampleCookie"];
if (exampleCookie != null)
{
int tempIndex = 0;
foreach (ListViewItem item in catListView.Items)
{
Response.Write("<br/>" + exampleCookie["cat" + tempIndex.ToString()]);
tempIndex = tempIndex + 1;
}
}

dropdown value selecting only first value

In the below C# code even though I am choosing my own year but still the value is passed as 1920 only. I can see all the values being displayed in the dropdown box but when I choose a value and submit it only 1920 is being passed to the database.
protected void Page_Load(object sender, EventArgs e)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}
Can anyone tell me where did I do mistake?
Because when you click on the button it is again executing the code in your Page_Load event. Solution is to set the dropdown values only once. You can use Page.IsPostBack property to check whether this is the initial load or a postback.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
}
Try like this;
if(!IsPostBack)
[
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add(i.ToString());
}
}
Try also setting a value for the selected items like this, make sure to check for postbacks
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList3.Items.Clear();
for (int i = 1920; i <= 2000; i++)
{
DropDownList3.Items.Add( new ListItem(i.ToString(), i.ToString()));
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
sbtc.dex(DropDownList3.SelectedItem);
}

Categories