webform that automatically checks button if another button is checked - c#

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

Related

Disable the NavigateUrl property on the HyperLink in c#

I need disable the NavigateUrl property on the HyperLink when the value of variable aut is less than zero.
I have tried this in code-behind of my .cs page, without success because the NavigateUrl property on the HyperLink is enabled, although it opens a blank page ( on window popup ) in the browser, when click on ImageUrl
Can anybody help me?
Thanks in advance
My code below :
.cs
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (aut > 0)
{
HyperLink button = (HyperLink)e.Row.FindControl("MMM");
button.Enabled = true;
}
else
{
HyperLink button = (HyperLink)e.Row.FindControl("MMM");
button.Enabled = false;
button.NavigateUrl = "";
}
}
}
.aspx
<asp:TemplateField HeaderText="MMM" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:HyperLink ID="MMM" runat="server" NavigateUrl='<%# (String.IsNullOrEmpty(Eval("MMM").ToString()) ? String.Format("http://...?sId={0}&s=2", HttpUtility.UrlEncode(Base64ForUrlEncode(Eval("id").ToString()))) : "") %>'
ImageUrl='<%#(String.IsNullOrEmpty(Eval("MMM").ToString()) ? "/Images/bullett/redbul.gif" : "/Images/bullett/forestbul.gif")%>'
ToolTip='<%#(String.IsNullOrEmpty(Eval("MMM").ToString()) ? "Not Exists" : "Exists")%>'
Target="_blank" BorderStyle="None" ForeColor="Transparent" OnClick="if (!confirm('Confirm ?'))return false;window.open(this.href,'playsample','width=500,height=500,left=100,top=100,scrollbars=yes,dependent=yes,toolbar=no,location=no,status=no,directories=no,menubar=no,status=no,resizable=yes');return false;"
Enabled='<%#(!String.IsNullOrEmpty(Eval("MMM").ToString()) ? false : true)%>'>
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
All those properties you're setting on your controls in your codebehind are being overwritten by the code within your <% %> tags in your aspx. For example, whatever you set button.Enabled in your C# code, will be overwritten later on in the page lifecycle by this:
Enabled='<%#(!String.IsNullOrEmpty(Eval("MMM").ToString()) ? false : true)%>'
I'm guessing that all the code like this in your aspx was written by someone else, before you started working on this project? If so, the easiest solution is probably to remove your GridView1_RowDataBound function, and instead modify the code inside the <% %> tags to change the behavior to what you need.
That being said, I will say that the way you're TRYING to do it is actually more correct. If you have the luxury of time, and you aren't afraid to refactor that existing code, you could instead move all that logic inside the <% %> tags to your GridView1_RowDataBound function, and remove the <% %> tags from your aspx so that it would just be:
<asp:TemplateField HeaderText="MMM" ItemStyle-HorizontalAlign="Center" ItemStyle-CssClass="ddl_Class_new">
<ItemTemplate>
<asp:HyperLink ID="MMM" runat="server">
</asp:HyperLink>
</ItemTemplate>
Of course, as I said, you would still need to recreate all of that removed logic in your GridView1_RowDataBound function, which I haven't bothered to show here.

Import checked status from a function

I'm trying to make this code work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%# status("de_cancel") %>'></asp:CheckBox>
What i'm trying to do is to retrieve the answer from the status function (which returns bool when i give a string) that i created in the c# source.
Doesn't give me compile error, but doesn't work. Edit: And btw, this is inside in a GridView
This works:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# Bind("de_cancel") %>'></asp:Label>
But this is NOT what I'm looking for.
Sorry about my bad English.
I'm assuming that this is in a grid view, or some other repeater.
In that case, try this:
<asp:Label ID="lblInfo" runat="server" Visible="true" Text='<%# status(Eval("de_cancel")) %>'></asp:Label>
this is essentially calling your status() method and passing in the value of de_cancel
You may have to convert de_cancel inside your status method though as Eval returns an object.
Just do this in the codebehind:
protected void Page_Load(object sender, EventArgs e)
{
//Get your string variable and use it as input here
statusChk.Checked = status("de_cancel");
}
protected bool status(string strStatus)
{
if (strStatus == "de_cancel")
{
return true;
}
else
{
return false;
}
}
This will occur when the page loads. There are other event handlers you may want to use - see http://msdn.microsoft.com/en-us/library/6w2tb12s(v=vs.90).aspx
Here is an example of how an event handler (OnSelectedIndexChanged) can be used to set a checkbox when a drop-down value is changed. As the accepted answer points out, you will need to set the AutoPostBack="true" option on the control if you want the event handler method to fire (and the page to refresh) when a control is changed:
Getting a dropdownlist to check a checkbox in asp.net/C#
Also, just looking at your code it looks like you might be using databinding, e.g. with a Repeater or ListView. In this case, please the examples for the ItemDataBound event handler below:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listview.itemdatabound(v=vs.110).aspx
http://www.codeguru.com/csharp/.net/net_asp/tutorials/article.php/c12065/ASPNET-Tip-Use-the-ItemDataBound-Event-of-a-Repeater.htm
Thanks to Darren tip, i was able to do it. Here what i did to make it work:
<asp:CheckBox ID="statusChk" runat="server" Visible="true" Enabled="false" Checked='<%#status(Convert.ToString(Eval("de_cancel")))%>'></asp:CheckBox>
Thanks everyone. ;)

How to change button's onclientclick property server side?

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).

How to remove disabled attribute from button?

How to remove disabled attribute from asp.net button from code-behind.
I have made disbled attribute to be true for button from javascript and want to enable it in code-behind.
In Javascript,
btnCalc.disabled=true;
In Code-behind,in the checkbox changed event,I am making it to enable,
btnCalc.enabled=true;//but this does not enable the button.
I tried to remove attributed for button,but it didt works for me.
btnCalc.Attributes.Remove("disabled");
Please,let me know,any other way to achieve this.
Hi Maha i have Tried this and Got Working.,
JavaScript:
<script type="text/javascript">
function handleChange(cb) {
var btn = document.getElementById('ButtonTest');
if (cb.checked == true) {
btn.disabled = true;
alert('Checked True');
return true;
} else {
alert('False');
btn.disabled = false;
return false;
}
}
</script>
ASPX Page Code:
<div>
<asp:CheckBox runat="server" ID="checkMe" title="Hey Check Me" name="Check Me" ClientIDMode="Static" onclick="handleChange(this)" AutoPostBack="false" Text="Check Me" />
<asp:Button runat="server" ID="ButtonTest" title="Hey Check Me" ClientIDMode="Static" Text="Submit Button" OnClick="ButtonTest_Click" />
</div>
Code Behind:
protected void ButtonTest_Click(object sender, EventArgs e)
{
// Your Logic
}
But the Check Box AutoPostBack="false" should be in false. Other wise the Entire page will be reloaded and disabled will attribute will removed.
Hope it may helpful., :)
You may need some knowledge of ASP.NET page life circle
You enabled the button from code-behind, it just help generate a response text to browser, the JavaScript will run after browser load HTML(the response text), you JavaScript code run and disable the button again.
You can register a startup script to enable the button after you disable button script

ASP.net checkbox always checked

<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
}

Categories

Resources