When page loaded Checkbox only check one time ? ASP.NET ,C# - javascript

My simple question is that ,is there any method/way in which 'Checbox' only check one time.I mean when page loaded which contains 'checkbox' ,after page loaded then i select check box and press 'button' then checkbox will be disabled,only one time checkbox will tick.Is there any way?
Thanks.
here is my code :
<label>Allow Null <asp:CheckBox ID="Null" runat="server" /></label>
<label>Primary Key <asp:CheckBox ID="Primary" runat="server" />
<br />
<asp:Button ID="Button1" ValidationGroup="s" onclick="Button1_Click" runat="server" Text="Insert" />
I want when i press "Insert" button then primary checkbox(check its tick or not),if Primary cehckbox tick then next time this will be disabled.Only one time "Primary" checkbox tick.

As the coments suggests ,
from client script you can try this:
$("#checkId").attr("disabled", true);
if you are in asp.net web-forms and the button does postback then you can even disable it from code behind.
Edit
<label>Allow Null <asp:CheckBox ID="Null" runat="server" ClientIdMode="Static" /></label>
<asp:Button ID="Button1" ValidationGroup="s" onclick="Button1_Click" runat="server" OnClientClick="UpdateCheck();" Text="Insert" />
<script>
function updateCheck(){
$("#Null").attr("disabled", true);
}
</script>

Related

asp.net Control visibility

I have a aspx form in which there are 5 controls below is the design code:
Question
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br />
Answer
<asp:DropDownList ID="DropDownList2" runat="server"></asp:DropDownList>
<asp:CheckBox ID="CheckBox1" runat="server" Text="If Other" AutoPostBack="True"
OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
A user selects a question from the drop down list and chooses the answer from the answer list.
If he/she doesn't find the answer in drop down list than he/she selects the checkbox and type the answer in textbox and press the submit button afterwards.
Is it possible to show the textbox and at the same time hide the drop down list using a single line code in checkbox OnCheckedChanged event?
Yes (though I'd use some more lines for better readability), you should set the visibility of the TextBox to false, to hide it at first:
<asp:TextBox ID="TextBox1" runat="server" Visible="false"></asp:TextBox>
In CheckBox1_CheckedChanged, you add the following lines:
DropDownList2.Visible = !CheckBox1.Checked;
TextBox1.Visible = CheckBox1.Checked;

Button in asp.net doesn't work sometimes

I have a textbox and a button in my code.The button when clicked should validate the value entered in the textbox.The button most of the times does it but at times doesn't do anything at all.Any help would be appreciated.
<asp:Button ValidationGroup="vgpPno" runat="server" ID="btnSubmitPartNumber" OnClick="btnSubmitPartNumber_Click" SkinID="MSWButton" Text="OK" OnClientClick="if(Page_ClientValidate(){startPLI();}meta:resourcekey="btnSubmitPartNumberResource1" />
<asp:TextBox runat="server" ValidationGroup="vgpPno" SkinID="MSWTextBox"
ID="tbPARTNUMBER" MaxLength="11" meta:resourcekey="tbPARTNUMBERResource1" Width="230" Style="display: inline" CssClass="form-control input-sm"></asp:TextBox>
<asp:Button ValidationGroup="vgpPno" runat="server" ID="btnSubmitPartNumber" OnClick="btnSubmitPartNumber_Click" SkinID="MSWButton" Text="OK"
OnClientClick="if(Page_ClientValidate(){startPLI();}meta:resourcekey='btnSubmitPartNumberResource1'" />
Check Your web Form. If you are using form tag more then one time asp.controls not working properly

How to trigger TextChange event without leaving focus in ASP.NET C#?

I tryed trigger the TextChange event with ajax but it dosnt realy work how i do it. Hope you guys can help me.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="0">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" AutoCompleteType="Disabled" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
The ScriptManager should appear before the UpdatPanel, not inside of it. And you don't need to define TextBox1 as a trigger. The TextBox being inside the UpdatePanel and the property ChildrenAsTriggers being true by default, the panel will be updated when TextBox1 loses focus after its content has been modified.
UPDATE
I originally misread the question. The following posts describes a method to trigger a postback and refresh the UpdatPanel every time a key is pressed in the TextBox: How do I make a Textbox Postback on KeyUp?.

Not getting ClientID in ASP.Net

I have ASP.Net page where i am calling a JavaScript function like this:
Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value)" Text ="GO!" />
But on clicking the GO button, i am getting the following error:
JavaScript runtime error: Unable to get property 'value' of undefined or null reference
On viewing the rendered code for the button:
<input type="submit" name="btn_view" value="GO!" onclick="return AlertOnGo('View Configuration',document.getElementById('<%= txt_name.ClientID %>').value);" id="btn_view" />
It is not putting the appropriate ClientID. I tried setting the CLientIDMode to static for the test box, yet no help.
This question is similar to this unanswered question.
There are a few solutions to this problem.
One of the simpler would be to move the name into a JavaScript variable since the problem only occurs within the control when trying to evaluate txt_name.ClientID inside the OnClientClick attribute of an ASP.NET control.
<script>
var txtName = '<%= txt_name.ClientID %>';
</script>
Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click"
OnClientClick="return AlertOnGo('View Configuration',
document.getElementById(txtName).value)" Text ="GO!" />
you can use the property ClientIDMode="Static" inside the asp:textbox tag
like this
<asp:TextBox ClientIDMode="Static" ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click"
OnClientClick="return AlertOnGo('View Configuration',
document.getElementById(txtName).value)" Text ="GO!" />
this way the client id of the text box would be same as the "id" and you can use
document.getElementById('txt_name').value
and it works for me
Youre using <%= %> in the property of an asp.net web control - I wouldnt expect that to work, since the whole control will be replaced by html.
I would move your javascript out into a script block and attach the event handler in code rather than putting it all inline.
Enter server name: <asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" Text ="GO!" />
<script>
document.getElementById('<%= btn_view.ClientID %>').attachEvent('click', function() {
return AlertOnGo('View Configuration',
document.getElementById('<%= txt_name.ClientID %>').value);
});
</script>
-- edited answer --
aspx
<asp:Button ID="btn_view" runat="server" OnClick="View_btn_click" OnClientClick="javascript:return AlertOnGo('View Configuration');" Text ="GO!" />
script
function AlertOnGo(mode)
{
var btnId = '<%= txt_name.ClientID %>';
var value=document.getElementById(btnId).value;
//your code
return false;
}
I prefer to use String.Concat. You can use \u0027 instead of apostrophe.
OnClientClick='<%#String.Concat("document.getElementById(\u0027", AddToCartBtn.ClientID.ToString(), "\u0027).value = \u0027Adding...\u0027;")%>'
Another solution is you can go to browser do insect element read the dynamic Id of textbox and set that in javascript like:
var ddlFunction = document.getElementById('ctl00_SPWebPartManager1_g_ecede3f6_2b87_405a_af1b_70401bb8d4c7_ddlFunction');
Html:
<asp:DropDownList ID="ddlFunction" runat="server" CssClass="dropDownClass">
<asp:ListItem Selected="True">Select One</asp:ListItem>
<asp:ListItem>Sample 1</asp:ListItem>
<asp:ListItem>Sample 2</asp:ListItem>
</asp:DropDownList>
You can create a css class and use that instead of client id, (with jQuery $('.txt_name').text()):
Enter server name:
<asp:TextBox ID="txt_name" CssClass="txt_name" runat="server"></asp:TextBox>

calling client side event on button click in GridView

I need to call a javasript function when a button is clicked in my GridView. Is it possible to do this, and if so does somebody have an example?
<asp:TemplateField>
<asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:alert('hello');" />
</asp:TemplateField>

Categories