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);
}
Related
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();
}
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();
}
I need to focus TextCells one by one via a button click.
I tried listView.ScrollTo.
private void Button_Clicked_1(object sender, EventArgs e)
{
listViewJson.ItemTapped += ListViewJson_ItemTapped;
}
private void ListViewJson_ItemTapped(object sender, ItemTappedEventArgs e)
{
var focusing = e.Item;
listViewJson.ScrollTo(focusing, ScrollToPosition.MakeVisible, true);
}
Firstly, try to define an index to detect which cell to be selected. Then change the index via button click like:
int selectedIndex = 0;
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
when the TextCell is selected the ListView's ItemSelected event will fire, you can put your code in it like:
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
Here is my code behind for you referring to:
ObservableCollection<string> dataList = new ObservableCollection<string>();
int selectedIndex = 0;
public MainPage()
{
InitializeComponent();
for (int i=0; i<10; i++)
{
dataList.Add("item" + i);
}
myListView.ItemsSource = dataList;
myListView.ItemSelected += MyListView_ItemSelected;
MyBtn.Clicked += MyBtn_Clicked;
}
private void MyListView_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
//try to do something
}
private void MyBtn_Clicked(object sender, EventArgs e)
{
if (selectedIndex == dataList.Count) selectedIndex = 0;
myListView.SelectedItem = dataList[selectedIndex++];
}
subj. I get the ID, and it will fill the table. But in this example, only the last entry is filled.
If you announce in page_load "pro = number" - that is filled correctly.
What to do please tell me.
public string idul;
public int pro;
public string nz;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
pro = 5;
}
protected void Button2_Click(object sender, EventArgs e)
{
for (int i = pro; i > -1; i--)
{
wisp();
}
}
public void wisp()
{
SqlConnection con_tsp = new SqlConnection(WebConfigurationManager.ConnectionStrings["JJ"].ConnectionString);
string tsp1 = "UPDATE TSP SET IDUL=#IDUL WHERE NZ=#NZ";
SqlCommand upd_tsp = new SqlCommand(tsp1, con_tsp);
upd_tsp.Parameters.AddWithValue("#NZ", pro);
upd_tsp.Parameters.AddWithValue("#IDUL", "12");
upd_tsp.Connection.Open();
upd_tsp.ExecuteNonQuery();
upd_tsp.Connection.Close();
pro--;
}
You have set Pro as a global variable, on Button2_Click the value will be set to 0 (int's default value)
and then the button click handler will be called hence u r getting only 0 as the input value
Now there are 2 buttons, let's call them "Button1" and "Button2" respectively.
If I click "Button1", store "1" in ViewState["Record"]; also, if I click "Button2",
store "2" in ViewState["Record"], etc.
When I click Button1⟶Button1⟶Button2⟶Button2, my expected result is: in ViewState["Record"], there are a list of records, like
[1,1,2,2].
Here is my click event code:
protected void Button1_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "1");
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState.Add("Record", "2");
}
//show the viewstate result
protected void ShowResult(object sender, EventArgs e)
{
for (int i = 1; i <= 4; i++)
{
Label.Text += ViewState["Record"];
}
}
It shows "2222" instead of "1122".
How can I code to resolve it?
When you add Record key a value actually it not adds, just puts a value Record key. You always see last added value on your loop.
You should add a List to ViewState and add value to list.
Add this property
protected List<long> ViewStateList
{
get{ if(ViewState["Record"] == null) ViewState["Record"] = new List<long>();
return (List<long>)ViewState["Record"] }
}
And use like
protected void Button1_Click(object sender, EventArgs e)
{
ViewStateList.Add(1);
}
Loop
protected void ShowResult(object sender, EventArgs e)
{
foreach(long item in ViewStateList)
{
Label.Text += item.ToString();
}
}