<asp:CheckBox ID="isSubscribed" runat="server" /> Subscribe to mailing list?<br /><br />
<asp:Button runat="server" CssClass="btn" ID="btnPrefUpdate" OnClick="updatePrefs" Text="Update" /><br /><br />
This fires in the code behind:
protected void updatePrefs(object sender, EventArgs e)
{
Response.Write(isSubscribed.Checked);
Response.End();
}
But it's always coming out as true! Whether it is checked or not! I know I'm doing it wrong, could someone show me how to access this value properly?
like #Curt said, it looks to me like you have something in your page_load. if you set the value in Page_Load make sure it is inside the following if statement
if(!Page.isPostBack)
{
isSubscribed.Checked = true;
}
You are doing it the right way. The boolean property Checked should just say True or False (I even tested it). Is your Page_Load doing something with the checkbox? In other words, is the value of the checkbox somehow (re)set when the post back is occuring (the postback of the button click).
In your Page_Load method you could include:
if (!this.IsPostBack)
{
// Set default or loaded values for controls
}
Related
In my ASP.NET page's Page_Load I'm trying to determine whether a certain button has been clicked and is attempting a postback:
if (Page.IsPostBack)
{
if (Request.Params.Get("__EVENTARGUMENT") == "doStuff")
doSomething();
}
doStuff is a JavaScript within the markup, but I don't want this alone to trigger the doSomething() method call, I also need to be able to ensure that the button the user has clicked is correct.
How can I identify and reference the button control from the code behind once the user clicks? I searched for and discovered this but when I try to implement it, the control returned is always null.
Or use the CommandName and command events.
<telerik:RadButton ID="RadButton1" runat="server" CommandName="first" CommandArgument="one" OnCommand="CommandHandler" />
<telerik:RadButton ID="RadButton2" runat="server" CommandName="second" CommandArgument="two" OnCommand="CommandHandler" />
<asp:Label ID="Label1" Text="" runat="server" />
<asp:Label ID="Label2" Text="" runat="server" />
protected void CommandHandler(object sender, CommandEventArgs e)
{
Label1.Text = e.CommandArgument.ToString();
Label2.Text = e.CommandName;
}
You haven't really explained what you're trying to do, but it sounds like you would have a much easier time if you added an OnClick event handler to the button instead of trying to figure it out in Page_Load. The event handler would only be executed when that button is clicked.
I have a button as follows:
<asp:Button ID="btnNext" runat="server" Font-Bold="true" Text="Next"
onclick="btnNext_Click" style="text-align: center" Width="80px" />
and a RadNumericTextBox:
<telerik:RadNumericTextBox ID="txtTotal5" runat="server" Width="50px" AutoPostBack="true"
MinValue="0" ontextchanged="txtTotal5_TextChanged"><NumberFormat DecimalDigits="0" /></telerik:RadNumericTextBox>
I need to set the onClientClick property based on whether a telerik RadNumericTextBox has text in it or not. If it does not have a value, the onClientClick property needs to be set as shown below. If there is a value in the box, I want to just go on to the onclick event which directs it to the next form.
protected void txtTotal5_TextChanged(object sender, EventArgs e)
{
if (txtTotal5.Value.ToString() == "")
{
btnNext.OnClientClick = "javascript: return confirm('Please have the employee complete this form.')";
}
else
{
btnNext.OnClientClick = "";
}
}
Now I have used the debugger several times to step through the code, and the value changes as I expect within the function, but even when it sets the OnClientClick property to "", the box still pops up when the button is clicked. Is the value not being passed to the client somehow? Any suggestions would be appreciated. Thanks in advance!
You need to add the OnClientClick to the Attributes collection of the button, like this:
btnNext.Attributes.Add("OnClientClick", "YourJavaScriptFunction();");
I have found a solution for my issue. I left the function above as is, but I basically copied the if-else into my page_load event inside if(IsPostBack).
I have session in a page and would like to reset this session when the user text input changes. For example: account number xxx will have a session, once account number changes to yyy i would like to reset session. I dont have login to this page so cannot dump session upon logout. help is appreciated. thank you in advance.
Try this:
in aspx:
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
in code behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session.Clear();
}
So you'll need an event handler that is fired when the account number changes (or whenever the user text input changes), and then according to this post you'll need to either use Session.Clear() which just removes all values from the object or Session.Abondon() which will destroy the session and trigger the Session_OnEnd event. Whichever you use will depend on what you want to accomplish.
make ajax call on "onkeyup" event of textbox and set new value in that session, you will get new value of textbox and can set it in session.
Use TextChanged event of TextBox and destroy the session using Session.Abandon() or Session.Clear() method
This will help you
If you write down code here, then we can give you exact answer
try this
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
code behind
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session["your_sesion_name"]=TextBox1.Text;//or what ever you want to update value
}
There are a couple of ways to do what you are asking.
Option one:
You could uses the text boxes ontextchanged event
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
in code behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Session.Clear();
}
The pit fall here is the OnTextChanged event is only fired once the textbox has lost focus.
Option 2:
Uses JavaScript to capture the onkeyup event and make an ajax call back to the server to update your session information
The pit fall here is that every time the user enters a character into the textbox you would be making the ajax call. Although you could add checks so that the call is only made if the account number is a certain length.
Option 3:
You could add a button to your form for the user to click once they have entered the account number. This would avoid needless calling the server on every key press as in option 2. Then within you buttons click event you could do the work with your session
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click" Text="Submit" />
Code behind
protected void btnSubmit_Click(object sender, EventArgs e)
{
// do session work
}
I have following codes in asp.net:
<asp:dropdownlist id="ddlApp" runat="server" />
<asp:button id="btnSmt" runat="server" Text="Submit" />
and code behind:
private void btnSmt_Click(object sender, System.EventArgs e)
{
lbl.Text = ddlApp.SelectedItem.Value;
}
The logic is very simple. Get the selected value of dropdownlist and pass it to lbl.text.
But the problem is no matter how I try, the text show the fist value of list in the dropdownlist rather than the selected value.
And I notice that everytime I click the button the page refresh.
Please help.
BTW, I have the following event binding:
private void InitializeComponent()
{
this.btnSmt.Click += new System.EventHandler(this.btnSmt_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.ddlApp.SelectedIndexChanged +=new System.EventHandler(this.ddlApp_Change);
}
You have to do the binding for the dropdownlist in
if (!Page.IsPostBack)
Else it will re-build the items for the dropdown list on every postback and
therefore only return the currently selected item in the new collection - which is the first.
It also looks like you're missing the btnSmt_Click on the button - but you've probably set it somewhere else...
First of did you debug this??? Cause the C# code seems corrent.
Try changing this:
<asp:button id="btnSmt" runat="server" Text="Submit" />
To
<asp:button id="btnSmt" runat="server" Text="Submit" OnClick="btnSmt_Click" />
if this truely is your code your click event would never have been caught, therefor if you would put a breakpoint in your C# code you would have seen that the action is not triggered.
Anyway, hope it helps
I'm trying to create a webform in asp.net/c# that uses checkboxes. What I would like to do is automatically check one box if another is checked
for example if button2 is checked, automatically check button1, but don't automatically check if button1 is checked
tried doing an if statement that handles this like:
if(checkbox1.changed == true)
{
checkbox2.changed == true;
}
but this didnt work. anyone point me in right direction of where to look on how to do this.
You mean something like this?
default.aspx
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="True"
oncheckedchanged="CheckBox1_CheckedChanged" Text="Box1" />
<asp:CheckBox ID="CheckBox2" runat="server" Text="Box2" />
</form>
default.aspx.cs
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
if (CheckBox1.Checked)
CheckBox2.Checked = true;
}
This will check your checkbox2 if you check checkbox1. Draw your attention on AutoPostBack="True" which causes your page to postBack to the server if you change checkbox1.
Anyway you should think over this kind of solution. I don't know exactly what you want to achieve, but it may be a better solution to manage this on client via javaScript.
You can do this entirely on Javascript
<asp:CheckBox ID="CheckBox1" runat="server" onclick="changed(this);" />
<asp:CheckBox ID="CheckBox2" runat="server" />
And then the JS function that will check Checkbox2 if Checkbox1 is checked....
function changed(element) {
if (element.checked) {
document.getElementById('<%=CheckBox2.ClientID%>').checked = element.checked;
}
}
you are doing a simple mistake. Checkboxes have a Checked property not changed.
if( checkbox1.Checked == true)
{
checkbox2.Checked == true;
}
Read more about CheckBox properties on MSDN