very simple c# submit button - c#

I have several ImageButtons on the top of my page, then I have a textbox and button in the middle. If you type in the textbox then hit enter on the keyboard the browser follows the link of the first ImageButton instead of the submit button next to the textbox. I have ran into this in the pase and had to put the imagebuttons on the bottom of the page for it to work correctly, that is not an ok fix in this case. I tried setting UseSubmitBehavior="true" , but that does nothing. I tried putting the textbox and button in a separate DIV and a separate panel, didn't work either
TOP of the page
<div style="position:absolute; left: 70% ; top: 5%;">
<asp:ImageButton ID="imgFB" runat="server" ImageUrl="Images/facebook_icon.jpg" PostBackUrl="http://www.facebook.com/832586561" />
<asp:ImageButton ID="imgLI" runat="server" ImageUrl="Images/linkedin_logo.jpg" PostBackUrl="http://www.linkedin.com/pub/scott-selby/33/304/44a" />
<asp:ImageButton ID="imgCB" runat="server" ImageUrl="Images/careerbuilder_logo.jpg" PostBackUrl="http://www.careerbuilder.com" />
<asp:ImageButton ID="imgCP" runat="server" ImageUrl="Images/codeplex_logo.jpg" PostBackUrl="http://www.codeplex.com" />
</div>
Middle of Page
<div ID="formPanel" runat="server" style="position:absolute; top:235px;">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" UseSubmitBehavior="true" />
</div>

You may set the default button via <form/> attribute.
<form defaultbutton="btnChangeCity" id="form1" runat="server">
...
</form>
Or use Panel control to set default button.
<asp:Panel ID="Panel1" runat="server" DefaultButton="btnChangeCity">
<asp:TextBox ID="txtNewCity" runat="server"></asp:TextBox>
<asp:Button ID="btnChangeCity" runat="server" Text="Change City" />
</asp:Panel>

You surround your controls with <div> which is a good idea, but to have it run at all you also need <form>. In case you just didn't include it, the submit button also has to have OnClick="sub" attribute.
Read more here.

Related

Textbox gets validated by two buttons

I am making some web app using ASP.net and for look of the site I am using Bootstrap. But I have this problem that I can't solve by googling
So this is the deal .. I have panel and inside I have simple textbox and button
<asp:Panel runat="server" ID="pnlCompany">
<div style="display: inline-flex; overflow: hidden; float: left">
<asp:TextBox ID="txtClient" runat="server" CssClass="text-primary"
Wrap="true" Width="300px" required=""></asp:TextBox>
<asp:Button ID="btnInsertClient" runat="server" OnClick="btnInsertClient_Click"
CssClass="btn btn-primary" data-target="#MainContent_pnlCompany_txtClient"
Text="Dodaj klijenta" />
</div>
</asp:Panel>
But my problem show up here ... Under that panel I have another panel, with Button (that works fine, it collapsing table at end of page), then I have FileUpload button and Button (btnUploadUsers) that is giving me hard time, idea of this button is to go with upload of file, but once i click it, it keeps validating textbox (txtClient) from above panel ... I am sure this button fire it's event "btn_UploadUsers_click" because once i enter data in textbox, program stop on my break point
<asp:Panel runat="server" ID="pnlImportUsers">
<div style="display: inline-flex; overflow: hidden; float: left">
<asp:Button ID="btnCompanyList" runat="server" class="btn btn-lg btn-primary"
data-target="#MainContent_pnlCompanyList" Text="Lista klijenata" />
<asp:FileUpload ID="fuUsersUpload" runat="server" Enabled="false"
CssClass="btn-group-vertical" />
<asp:Button ID="btnUploadUsers" runat="server" class="btn btn-lg btn-primary"
Text="Upload" OnClick="btnUploadUsers_Click" data-target="#MainContent_UsersId"
ValidationGroup="fuUsersUpload" />
</div>
</asp:Panel>
Let me know if I can do anything to describe this problem better, thank you
Thank you #Benni_mac_b for advice, but i figured out what was a problem, so I'll post answer in case anyone else be in this situation.
So with using Bootstrap css and js, as you can see 'required' on the form, it will always be check if has value on every post_back
<asp:TextBox ID="txtClient" runat="server" CssClass="text-primary"
Wrap="true" Width="300px" required=""></asp:TextBox>
So, as I didn't wanted my btnUploadUsers to do PostBack, simply adding
UseSubmitBehavior="false"
In my code snippet fixed my problem

How to execute event when user presses return on web page

I have an ASP.NET website and I have a forgot password page where the user enters their email address in a text box and when they click the button to retrieve password, the event runs as fine.
Only problem is if the user types in their email address and presses ENTER instead, it runs a search (I have a search bar at the top of the page) and so the result comes back as 'search query not found'. But this search bar at the top is on a different ASP.NET page.
So anyway, I want the event onclick to run when the user presses enter and not run a search query. Does anyone have any ideas? I've searched on this site but not really found the answer I need.
There are couple ways you can do this. If it is a webform and you have your textbox wrapped with an asp:panel you can do as below:
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
If its not a webform or you want to move away from that try below:
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
And in the codebehind fire your button click in the page load based on the parameters of the postback.
Specify the "defaultbutton" property to the ID of (event you want to fire).
You can specify the "defaultbutton" property at the Form level (in the form tag)
Or else you can define them at panel level in the tag.
The form level setting is overridden at the panel level setting.
The Event Handler for the specified button gets fired simulating a true submit button functionality.
<form id="sampleform" runat="server" defaultbutton="button1">
<div>
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<asp:Button ID="button2" runat="server" Text="Cancel" OnClick="Button2_Click" />
<asp:Button ID="button1" runat="server" Text="Ok" OnClick="button1_Click" />
<asp:Panel ID="panel1" runat="server" defaultbutton="Button5">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Button5" OnClick="Button5_Click" />
</asp:Panel>
</div>
</form>
In this example, Button1 is the default button for the form (Type something in Textbox 1 and hit enter, button1_Click gets fired). But for "Panel 1", default button will be Button 5 (Type in Textbox3 or Textbox 5 and hit enter).
You can have any number of panels with different default button for each panel.
Adding an ASP Panel around the text box and button with the id of the button name as the property for DefaultButton was the best way to do this.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>

asp:Button and asp:LinkButton and asp:Button submitting on enter in Chrome

In Internet Explorer if I hit enter in the TextBox P it submits the Form using the onclick event of LoginButton.
This is what I want to happen.
In Google Chrome if I hit enter it submits the form with the onclick of Button1.
This is not what I want to happen.
Button1 is actually not visible on the form under normal circumstances. It is made visible under certain circumstances to do a different task then login.
How can I force this, browser independently, to always use LoginButton onclick when someone presses enter?
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
onclick="LoginButton_Click" />
<asp:Button
ID="Button1" runat="server"
Text="Submit" onclick="Button1_Click" />
You set the forms default button:
<form id="Form1" defaultbutton="SubmitButton" runat="server">
The Following works for me.
<form id="form1" runat="server" defaultbutton="LoginButton">
<div>
<asp:TextBox ID="P" runat="server" TextMode="Password" Width="150"></asp:TextBox>
<asp:LinkButton CssClass="button" ID="LoginButton"
runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1"
OnClick="LoginButton_Click" />
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" > </asp:button>
</div>
</form>
You'll probably want to set a default button in your Page_Load method, like this:
this.Form.DefaultButton = this.LoginButton.UniqueID;
I ended up using C# in my Page_Load(object sender, EventArgs e) method to set the DefaultButton. Found this on another stackoverflow post.
https://stackoverflow.com/a/2213237/2907463
this.Form.DefaultButton = Login1.FindControl("LoginButton").UniqueID;

AjaxControlToolkit ModelPopupExtender displaying left side of the screen instead of center

This is my code for ModelPopupExtender:
<asp:View ID="ViewCDB" runat="server">
<div id="cdbupbar" style="padding:2px">
<asp:Button ID="btnCDB" runat="server" CssClass="CButton"
onclick="btnCDB_Click" Text="Create New Discussion Board" />
<asp:Panel ID="Panel13" runat="server">
<asp:Button ID="Button9" runat="server" Text="Cancel" CssClass="newButton" />
<uc2:CreateNewCDB ID="CreateNewCDB1" runat="server" />
</asp:Panel>
<asp:modalpopupextender id="ModalPopupExtender1" runat="server"
targetcontrolid="btnCDB" popupcontrolid="Panel13" CancelControlID="Button9"
backgroundcssclass="ModalPopupBG">
</asp:modalpopupextender>
</div>
</asp:View>
The problem is that, whenever I click btnCDB button, it shows the model popup but it is displayed on left hand side of the window instead in middle. I tried to add it in <div> and <td> and assigning their alignment property to "middle" but didn't worked. The container that holding this code is set to left aligned. I can't change it because its website's main container, changing its alignment will cause all website content to middle. That I don't want. Also I checked all CSSes, they didn't effect the alignment property.
<asp:Panel ID="Panel13" runat="server" style="margin-left:35%;">
try this and if it makes any difference, u better define a css class for your panel.

Unwanted FileUpload textbox clearing after submit in IE

Since there is no way to change FileUpload button text I hide FileUpload control and used my own fake button and textbox to be able to do it.
<script language="JavaScript" type="text/javascript">
function HandleFileButtonClick(sender) {
var parentDiv = $(sender).parent();
$(parentDiv).children('[id*=fuBoutiqueImage]').click();
}
function LoadFakeField(sender) {
$(sender).parent().find("input[id$='txtFakeText']").val($(sender).val());
}
</script>
<asp:Panel ID="pnlCommandButtons" runat="server" CssClass="commandButtons">
<div class="uploader">
<asp:Label ID="lblUploadFile" EnableViewState="false" runat="server" Text="<%$Resources:Common, BoutiqueGallery_UploadFile %>" />
<asp:FileUpload ID="fuBoutiqueImage" runat="server" style="" onchange="LoadFakeField(this);" />
<input ID="txtFakeText" type="text" name="txtFakeText" readonly="true" runat="server" />
<input ID="btnFakeButton" type="button" onclick="HandleFileButtonClick(this);" value="<% $Resources:Common, ButtonName_Browse %>" runat="server" />
</div>
<asp:Panel ID="pnlDeleteButton" class="delete" runat="server">
<ef:ButtonExt ID="btnDelete" runat="server" Text="<%$Resources:Common, BoutiqueGallery_Delete %>" OnClick="btnDelete_Click" CausesValidation="false" Color="Red" Icon="Delete" Width="60" />
</asp:Panel>
<div id="pnlAddButton" class="add">
<ef:ButtonExt ID="btnAdd" runat="server" Text="<%$Resources:Common, UploadImage %>" OnClick="btnAdd_Click" ValidationGroup="emailSend" Width="104" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="fuBoutiqueImage" ErrorMessage="<%$Resources:Common, Attachment_FileToLarge %>" Text="<%$Resources:Common, Attachment_FileToLargeTextBox %>" Display="Dynamic" OnServerValidate="CustomValidator1_ServerValidate" ValidationGroup="emailSend"></asp:CustomValidator>
<asp:RegularExpressionValidator ID="FileUpLoadValidator" runat="server" ErrorMessage="<%$Resources:Common, Attachment_FileFormat %>" Text="<%$Resources:Common, Attachment_FileFormatTextbox %>" ValidationExpression=".*(\.jpg|\.png|\.gif|\.tif|\.jpeg|\.JPG|\.JPEG|\.PNG|\.GIF|\.TIF)$" ControlToValidate="fuBoutiqueImage" Display="Dynamic" ValidationGroup="emailSend" />
</div>
<div class="isActiveCheckbox">
<asp:CheckBox ID="cbImageIsActive" class="chkItem" OnCheckedChanged="cbImageIsActive_CheckedChanged" Checked='<%# Eval("IsActive") %>' AutoPostBack="true" runat="server" Text="<%$Resources:Common, BoutiqueGallery_IsImageActive %>" />
</div>
</asp:Panel>
My btnFakeButton triggers FileUpload click action and after that the path/fileName is copied to fake textbox. Then I can click btnAdd and everything works fine in ff but not in IE.
In IE after choosing a file and closing dialog box the path/fileName is copied but when I press btnAdd (or click checkbox) the FileUpload textbox is cleared and nothing happens. After second press of btnAdd, btnAdd_Click starts but FileUpload is empty and it ends with error. Since I can't restore FileUpload textbox from txtFakeText is there any way to prevent FileUpload clearing?
This is how I did it, although I only have to design for IE so I haven't tested this in other browsers. Also, I used a label instead of a textbox to display the selected filename.
I have to give credit to this site for starting me on the right path: http://www.codeproject.com/Tips/296593/To-trigger-Choose-file-to-Upload-dialogue-box-with
Essentially, what I did was create two buttons, as you have done. I used CSS to make the file upload control button the same size as my "fake" button. Then, I set the z-index of my "fake" button to -1, and positioned it directly behind the file upload control button. Then, I set the opacity of the file upload control button to 0. This way, the "fake" button is not used and I do not run into the problem of the real file upload box being cleared when clicking submit. The last step, not detailed in the linked article, is to change the "onchange" for the file upload button to be a function that updates the value of the filename label.
Here is the code:
function updateUploadLabel() {
document.getElementById("<%= FileUpload1.ClientID %>").click();
document.getElementById("<%= lblFileName.ClientID %>").innerHTML = document.getElementById("<%= FileUpload1.ClientID %>").value;
document.getElementById("txtInstructions").disabled = "true";
document.getElementById("removeUpload").style.display = 'inline';
}
<asp:FileUpload id="FileUpload1" onchange="updateUploadLabel()" runat="server" style="position: relative; opacity: 0; filter: alpha(opacity = 0);left:0px;font-size:24px; z-index:50; width:100px;/*display:none;*/"/>
<input type="button" id="btnChooseDoc" value="Choose File..." onclick="updateUploadLabel()" style="height:30px; width:100px; z-index: -1; position:relative; left: -105px; top: -10px;" />
<asp:Label id="lblFileName" runat="server" text='No File Chosen' style="position:relative; left: -105px; top: -10px;"></asp:Label>

Categories