Define a submit button at run time
net webpage, it has two search fields with separate submit buttons.
I would like to allow the user to enter some data in one of the fields and then press the enter key and submit using the appropriate button. Right now the enter key is being sent for the first submit button, but I want this to change if there is data in the second textbox.
How can i do this?
Currently my code is:
<label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
<asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />
<label for="ctl00_cpMain_txtCustomerSearch">Customer Name:</label><asp:TextBox id="txtCustomerSearch" runat="server" CssClass="TextBox" />
<asp:Button ID="btnCustomerSeach" runat="server" Text="Search" onclick="btnCustomerSeach_Click" CssClass="button" />
a source to share
Look at the property DefaultButton
. You can wrap each of these label / button pairs in a panel and set this property to btnEmployeeSearch
and btnCustomerSearch
accordingly.
http://forums.asp.net/t/985791.aspx
<asp:Panel DefaultButton="btnEmployeeSearch" runat="server" id="Panel">
<label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
<asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />
</asp:Panel>
a source to share
You can wrap each section in a panel and use its defaultbutton property to specify a submit button for each textbox:
<asp:Panel runat="server" DefaultButton="btnEmployeeSeach">
<label for="ctl00_cpMain_txtEmployeeSearch">Employee Name:</label><asp:TextBox id="txtEmployeeSearch" runat="server" CssClass="TextBox" />
<asp:Button ID="btnEmployeeSeach" runat="server" Text="Search" onclick="btnEmployeeSeach_Click" CssClass="button" />
</asp:Panel>
<asp:Panel runat="server" DefaultButton="btnCustomerSeach">
<label for="ctl00_cpMain_txtCustomerSearch">Customer Name:</label><asp:TextBox id="txtCustomerSearch" runat="server" CssClass="TextBox" />
<asp:Button ID="btnCustomerSeach" runat="server" Text="Search" onclick="btnCustomerSeach_Click" CssClass="button" />
</asp:Panel>
a source to share
Here is an asp: Panel extension that allows a page to have multiple default buttons based
on the current focus.
It overrides the Panel OnLoad method and the DefaultButton property.
You can put your fields and buttons in separate custom panels and initiate their DefaultButton in Page_Load () or On_Load ()
Here is the user control:
public class DefaultButtonPanel:Panel
{
protected override void OnLoad(EventArgs e)
{
if(!string.IsNullOrEmpty(DefaultButton))
{
LinkButton btn = FindControl(DefaultButton) as LinkButton;
if(btn != null)
{
Button defaultButton = new Button {ID = DefaultButton.Replace(Page.IdSeparator.ToString(), "_") + "_Default", Text = " "};
defaultButton.Style.Add("display", "none");
PostBackOptions p = new PostBackOptions(btn, "", null, false, true, true, true, true, btn.ValidationGroup);
defaultButton.OnClientClick = Page.ClientScript.GetPostBackEventReference(p) + "; return false;";
Controls.Add(defaultButton);
DefaultButton = defaultButton.ID;
}
}
base.OnLoad(e);
}
/// <summary>
/// Set the default button in a Panel.
/// The UniqueID of the button, must be relative to the Panel naming container UniqueID.
///
/// For example:
/// Panel UniqueID is "Body$Content$pnlLogin"
/// Button UniqueID is "Body$Content$ucLogin$btnLogin"
/// (because it inside a control called "ucLogin")
/// Set Panel.DefaultButton to "ucLogin$btnLogin".
/// </summary>
/// <param name="panel"></param>
/// <param name="button"></param>
public override string DefaultButton
{
get
{
return base.DefaultButton;
}
set
{
string uniqueId = value;
string panelIdPrefix = this.NamingContainer.UniqueID + Page.IdSeparator;
if (uniqueId.StartsWith(panelIdPrefix))
{
uniqueId = uniqueId.Substring(panelIdPrefix.Length);
}
base.DefaultButton = uniqueId;
}
}
}
a source to share