Button in asp.net doesn't work sometimes - javascript

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

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;

Adding the resize function to an asp textbox

In an there is the function at the bottom right corner to drag and change the size of the box, is there a way of adding it to an asp textbox?
Textbox is as follows:
<div class="control">
<asp:TextBox runat="server" ID="TextBox1" Width="100%" EnableViewState="false" ViewStateMode="Disabled" />
<asp:Label runat="server" ID="Label1" Visible="false" ViewStateMode="Disabled" EnableViewState="false" AssociatedControlID="TextBox1" />
</div>
Set the attribute TextMode="multiline", you can still set Rows="1" if you want it initially to look like a textbox/text input.

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

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>

Field is validated when loosing focus

I'm doing validation of a textbox with RequiredFieldValidation.
I want it to validate the textbox only when a button is clicked, not when textbox loses focus.
Now, every time when I click anywhere else on a page it shows an error message.
Here is a code:
<asp:TemplateField HeaderText="Email">
<ItemTemplate>
<asp:TextBox runat="server" ID="txtEmail" Text='<%#Eval("CustEmail") %>' />
<asp:RequiredFieldValidator ControlToValidate="txtEmail"
ValidationGroup="grpEmail"
ErrorMessage="Must enter Email Address" runat="server"></asp:RequiredFieldValidator>
<asp:Button Text="Update Email" ButtonType="Button" CommandName="UpdateEmail"
ValidationGroup="grpEmail"
CausesValidation="true"
Visible="true" runat="server"/>
</ItemTemplate>
</asp:TemplateField>
I just need it to be validated when button is clicked. That's it.
What am I doing wrong?
Setting the attribute CausesValidation of the Textbox to false should do the trick.

switch from type="button" to asp:LinkButton

was trying to switch from button to hyperlink(previous post) but couldnt get it working so have decided to use the link button instead.
in my aspx page I am using
<script type ="text/javascript">
function showImg(url)
{
$("#imagePreview").attr("src", url);
}
</script>
<div style="height:50px; margin-top:25px; margin-bottom:25px;">
<img id="imagePreview" alt="" width="30" height ="30"></img>
</div>
then in my ascx page I am using
<input type="button" onclick='javascript:showImg("<%# FieldValueString %>")' />
this works fine, when the button is clicked the pic is displayed below however I want a link button instead.
Ive tried the examples on http://www.devmanuals.com/tutorials/ms/aspdotnet/linkbutton.html but no joy,
iv tried
<asp:LinkButton
ID="LinkButton1"
runat="server"
Text="Preview"
Font-Bold="True"
ForeColor="Maroon"
PostBackUrl='javascript:showImg("<%# FieldValueString %>")' />
but nothing happens when I click the button do I have to add anycode to the .cs file?
Please help
thaks
You are using the incorrect property. Try this:
<asp:LinkButton
ID="LinkButton1"
runat="server"
Text="Preview"
Font-Bold="True"
ForeColor="Maroon"
OnClientClick='javascript:showImg("<%# FieldValueString %>"); return false;' />

Categories