Asp.net define enter button action - c#

I have 2 buttons in my webpage.When i press "Enter",it auto perform "close_topic",but i want my enter button to perform "button_click".
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
protected void button_Click(object sender, EventArgs e)
{
}
protected void close_topic(object sender, EventArgs e)
{
}
tried to use <asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">,but not working.

Did you wrap your panel around the 2 buttons? As follows:
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonTT">
<asp:Button ID="btnLogout" Text="Close Topic" OnClick="close_topic" runat="server" />
<asp:Button ID="ButtonTT" Text="Click" runat="server" OnClick="button_click" />
</asp:Panel>
Edit
Based on how to set a default 'enter' on a certain button, you can do it using code also:
Me.Form.DefaultButton = Me.btn.UniqueID;
or
Me.Page.Form.DefaultButton = = Me.btn.UniqueID;
Replacing the Me.Page with whatever your page name is.

Related

.NET Validators doesn't work

I have simple page Page.aspx like this:
<asp:Content ID="Content2" runat="server">
<asp:PlaceHolder ID="phNewAddress" runat="server" />
</asp:Content>
In code behind is:
protected override void Page_Load(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}
AddressDetail.ascx is:
<asp:TextBox ID="address_name" runat="server" />
<asp:RequiredFieldValidator ID="rfv" runat="server"
ControlToValidate="address_name" ErorMessage="Required" ValidationGroup="save">*
</asp:RequiredFieldValidator>
<asp:DropDownList ID="address_state" runat="server" AutoPostBack="true" />
<asp:Button ID="btnSave" runat="server" Text="Save" ValidationGroup="save" OnClick="btnSave_Click" />
DropDownList address_state is binding from datasource.
Now. After page is loaded and I press Save button, validator doesn't work.
But if I change DropDownList, so fire postback, and then press Save button, validator works fine.
Can anyone help me?
I am guessing validation controls work with viewstate, Move the below code from Page_load event to Page_Init event and see if it works.
protected override void Page_Init(object sender, EventArgs e)
{
AddressDetail xControl = (AddressDetail)Page.LoadControl("AddressDetail.ascx");
phNewAddress.Controls.Add(xControl);
}

UpdatePanel and panel visibility asp.net c#

I have two panels. Each panel contains an updatepanel.
The first panel is a password Textbox.
I set the second panel's visibility on page_load to false.
If the user enters the correct password, the second panel should be visible and the first panel shouldnd.
The code:
<asp:Panel ID="passwordPanel" runat="server">
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" runat="server">
<ContentTemplate>
Geben Sie das Passwort ein:<br />
<br />
<asp:TextBox ID="txtPassword" AutoPostBack="false" runat="server" TextMode="Password"></asp:TextBox>
<br />
<br />
<asp:Button ID="btnConfirmPassword" runat="server" AutoPostBack="true" Text="Senden" CssClass="button" OnClick="btnConfirmPassword_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
<asp:Panel ID="panelUploadDownload" runat="server">
<h2>Upload Paketformeln CSV</h2>
<input type="file" id="myFile" name="myFile" />
<asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" CssClass="button" Text="Upload" />
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<br />
<asp:Label ID="lblStatus" runat="server" Text="statusLabel"></asp:Label>
<br />
<asp:Panel ID="panelChanges" runat="server" CssClass="pnlCSS">
<asp:Label ID="lblChangesHeader" runat="server" Font-Bold="True" ForeColor="Black" Text="Änderungen"></asp:Label>
<br />
<asp:Label ID="lblChanges" runat="server" ForeColor="#009900" Text="changes"></asp:Label>
<br />
<br />
<asp:Button ID="btnConfirm" runat="server" OnClick="btnConfirm_Click" CSSClass="button" Text="Änderungen bestätigen" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<br />
<br />
<h2>Download Paketformeln CSV</h2>
<p><asp:Button ID="btnDownloadCsv" runat="server" OnClick="btnDownloadCsv_Click" Text="Download CSV" CSSClass="button"/></p>
</asp:Panel>
And the C# code:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Text == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
Load Event
protected void Page_Load(object sender, EventArgs e)
{
mainController = new MainController();
setStatus("", Color.Black);
lblChanges.Visible = false;
lblChangesHeader.Visible = false;
btnConfirm.Visible = false;
panelChanges.Visible = false;
panelUploadDownload.Visible = false;
}
For some reason it doesnt work. Any clues? Triggers?
The SecureString class doesn't allow you to see the value; that's the whole point of it. If you want to be able to work with the value entered into the PasswordBox, use the Password member of PasswordBox instead of the SecurePassword member:
protected void btnConfirmPassword_Click(object sender, EventArgs e)
{
if (txtPassword.Password == "XX")
{
uploadDownloadPanel.Visible = true;
passwordPanel.Visible = false;
}
}
i just removed the updatepanel from the passwordPanel and it worked.
In your load event, do the following
if(IsPostback)
{
uploadDownloadPanel.Visible = false;
}
Actually, every time when you press button, you load event also triggered
You can set your UpdateMode to Always of update panel within panelUploadDownload panel.It will work.
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">

Validation to force user to fill all two textboxes or neither

I have two TextBox on my page, and i want when a user fill one of them i warn him to fill another textbox, i mean to force user to fill all two textboxes or neither.
How to implement this using Validators, if applicable?
I added this controll on my page
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:RequiredFieldValidator ControlToValidate="txt1" Enabled="True" ID="required_validator1" runat="server" Text="Required" Visible="True" ValidationGroup="T"/>
<asp:TextBox ID="txt1" OnTextChanged="TextBox1_TextChanged" runat="server" AutoPostBack="True" />
<asp:RequiredFieldValidator ControlToValidate="txt2" Enabled="True" ID="required_validator2" runat="server" Text="Required" Visible="True" ValidationGroup="T" />
<asp:TextBox ID="txt2" CausesValidation="False" Enabled="False" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Button" ValidationGroup="T" OnClick="Button1_Click" />
and in my Webform.aspx.cs :
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (txt1.Text.Length > 0)
txt2.Enabled = true;
}
and :
protected void Button1_Click(object sender, EventArgs e)
{
if (txt1.Text == string.Empty && txt2.Text == string.Empty)
{
required_validator1.Enabled = false;
required_validator2.Enabled = false;
}
}
i want when user did not write any thing on txt1 and then he click on button, the validator warning does not show him an let him to resume,but it does not work, how can i do ti?
Use the logic. Set Required Field validator on both textboxes, and disable the second textbox. If user enters anything in the 1st textbox then enable 2nd textbox.
You can implement it using javascript. Simply set the textchange event of first textbox and check if its text length is greater than 1 then enable 2nd textbox.
ASPX:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:RequiredFieldValidator ControlToValidate="txt1" Enabled="True" ID="required_validator1" runat="server" Text="Required" Visible="True" />
<asp:TextBox ID="txt1" OnTextChanged="TextBox1_TextChanged" runat="server" />
<asp:RequiredFieldValidator ControlToValidate="txt2" Enabled="True" ID="required_validator2" runat="server" Text="Required" Visible="True" />
<asp:TextBox ID="txt2" CausesValidation="False" Enabled="False" runat="server" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="txt1" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
C#:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if(txt1.Text.Length > 0)
txt2.Enabled = true;
}

DefaultButton in MultiView.ActiveViewChanged Event

I am using a multiview control in asp.net, and on ActiveViewChanged event I want set default button programmatically as of the selected view so am using code:
if(myMultiview.GetActiveview() == myView)
this.Page.Form.DefaultButton = btnDefault.UniqueID;
here btnDefault is exist in myView, even though it throws exception 'System.NullReferenceException'
please suggest me the solutions
I had similar type of problem, And i try this,
<asp:Panel id="myPanel" runat="server" DefaultButton="ButtonID">
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server" >
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:View>
<asp:View ID="View2" runat="server">
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</asp:View>
</asp:MultiView>
</asp:Panel>
</div>
</form>
protected void Page_Load(object sender, EventArgs e)
{
myPanel.DefaultButton = "Button1";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("View1 Button is clicked");
MultiView1.ActiveViewIndex = 1;
myPanel.DefaultButton = "Button2";
}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Write("View2 Button is clicked");
MultiView1.ActiveViewIndex = 0;
}
may be help you.

Modal popup not generating postback to page

<ajaxToolkit:ModalPopupExtender runat="server"
id="ModalPopupExtender1"
cancelcontrolid="btnCancel" okcontrolid="btnOkay"
targetcontrolid="Button1" popupcontrolid="Panel1"
drag="true"
backgroundcssclass="ModalPopupBG"
/>
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup"
onclick="Button1_Click" />
<br />
<asp:UpdatePanel ID="up" runat="server">
<ContentTemplate>
<asp:Button ID="Button2" runat="server" Text="Post Back"
onclick="Button2_Click" />
<asp:Label ID="Label1" runat="server" AutoPostBack="true" Text="Nothing has happened yet..."></asp:Label>
<asp:Panel ID="Panel1" runat="server" AutoPostBack="true">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="btnCancel" runat="server" Text="Canel"
onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
So I am trying to get the Label to have the contents of what the user typed in the textbox inside the modal popup. Right now the btnOkay is not causing this to work.
.cs file:
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
TextBox1.AutoPostBack = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "";
TextBox1.Text = "";
}
protected void Button2_Click(object sender, EventArgs e)
{
Label1.Text = "You clicked the button";
}
I do not want the page to post back at all, but just to update the hidden labels on the page once information is entered. How do I do this?
Edit:
Alright, this should do the trick I think.
<ajax:ModalPopupExtender runat="server" id="ModalPopupExtender1" targetcontrolid="Button1"
popupcontrolid="Panel1" drag="true" backgroundcssclass="ModalPopupBG" />
<asp:Button ID="Button1" runat="server" Text="Test Modal Popup" /><br />
<asp:Panel ID="Panel1" runat="server">
<div class="HellowWorldPopup">
<asp:TextBox ID="TextBox1" runat="server" />
<asp:Button ID="btnCancel" runat="server" Text="Canel" onclick="btnCancel_Click" />
<asp:Button ID="btnOkay" runat="server" Text="Okay" onclick="btnOkay_Click" />
</div>
</asp:Panel>
<asp:UpdatePanel ID="up" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Nothing has happened yet..." />
</ContentTemplate>
</asp:UpdatePanel>
.
protected void btnCancel_Click(object sender, EventArgs e)
{
TextBox1.Text = "";
}
protected void btnOkay_Click(object sender, EventArgs e)
{
Label1.Text = TextBox1.Text;
up.Update();
}

Categories

Resources