如何指定主要图像按钮"输入"在主页标题

时间:2015-08-12 13:22:13

标签: asp.net vb.net onclick master-pages

如果您想真正了解正在发生的事情,我的网站是 pbradleyonline.com。我有一个包含两个不同图像按钮的母版页文件。他们都有目的。如果单击,则将用户重定向到一个页面,而另一个按钮应该链接到旁边的搜索文本框。一旦"搜索"点击按钮,它应该带他们到另一个页面与结果。

问题在于,当文本框(也在标题中)具有焦点时,如果按下回车键,则会触发标题中的错误按钮并重定向到错误的页面。我也明白,这可能与图像按钮的控件有关,而不是标准的提交按钮,但我想将它们保留为图像按钮。

我尝试了多种解决方案,包括设置" DefaultButton"文本框以及文本框和正确按钮所在的Panel控件的属性。这不起作用。这是MasterPage代码。

<%@ Master Language="VB" AutoEventWireup="false" CodeFile="Site.Master.vb" Inherits="Site" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head runat="server">
<link rel="shortcut icon" type="image/x-icon" href="~/Pictures/PBradleyFavicon.ico" />

<title></title>
<link href="~/Styles/Site.css" rel="stylesheet" type="text/css" />



<asp:ContentPlaceHolder ID="HeadContent" runat="server">

</asp:ContentPlaceHolder>
<style type="text/css" runat="server">
.textBox
{
 text-align: right;
 margin-top:10px;
 margin-right:10px;
 float:Right;
}
</style>
</head>
<body>
<form runat="server" >
  <div class="header"  dir="ltr">
        <div class="title">
        </div> 
        <div class="Logo" >
        <table style="width: 100%; margin-bottom: 15px;">
                <tr>
                    <td align="left" valign="top">
                        <asp:ImageButton ID="ImageButton2" usesubmitbehavior="false" runat="server" Height="64px" 
                            ImageUrl="~/Pictures/PBradleyJCBLogo.jpg" 
                            style="margin-left: 0px" Width="321px" CausesValidation="False" 
                            TabIndex="3" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="hideSkiplink" DefaultButton="ImageButton1">

        <asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" EnableViewState="false"
            IncludeStyleBlock="false" Orientation="Horizontal">
            <Items>
                <asp:MenuItem NavigateUrl="~/" Text="Home" />
                <asp:MenuItem NavigateUrl="~/Used_Equipment.aspx" Text="Equipment">
                    <asp:MenuItem NavigateUrl="~/New_Equipment.aspx" Text="New Equipment"></asp:MenuItem>
                    <asp:MenuItem NavigateUrl="~/Used_Equipment.aspx" Text="Used Equipment"></asp:MenuItem>
                    <asp:MenuItem NavigateUrl="~/jcb.aspx" Text="JCB Skid Steers"></asp:MenuItem>

                </asp:MenuItem>
                <asp:MenuItem NavigateUrl="~/Sprayer_Parts.aspx" Text="Online Parts" >
                    <asp:MenuItem NavigateUrl="~/Sprayer_Parts.aspx" Text="Sprayer Parts" />
                    <asp:MenuItem NavigateUrl="~/Farm_Equipment_Teeth.aspx" Text="Ag Teeth" />
                <asp:MenuItem NavigateUrl="https://www.allpartsstore.com/index.htm?customernumber=VA2352" Text="Aftermarket Tractor Parts" />
                </asp:MenuItem>
                <asp:MenuItem NavigateUrl="~/Plastic_Supplies.aspx" Text="Plastic Supplies" />
                <asp:MenuItem NavigateUrl="~/Video.aspx" Text="Customer Corner">
                    <asp:MenuItem NavigateUrl="~/Forms.aspx" Text="Customer Forms"></asp:MenuItem>
                    <asp:MenuItem NavigateUrl="~/Video.aspx" Text="Video Lounge"></asp:MenuItem>

                </asp:MenuItem>
                <asp:MenuItem NavigateUrl="~/Contact.aspx" Text="Contact Us" />
            </Items>
        </asp:Menu>
    </div>
        <asp:Panel ID="p" runat="server" DefaultButton="ImageButton1">         
           <asp:ImageButton OnClientClick="ImageButton1_Click" ID="ImageButton1"  
                runat="server" CssClass="textBox" Height="22px" 
            ImageUrl="~/Pictures/SearchIcon2.png" OnClick="ImageButton1_Click" 
                Width="22px" TabIndex="1" />   
        <asp:TextBox ID="TextBox1"  runat="server" DefaultButton="ImageButton1" CssClass="textBox" 
            style="margin-top: 11px" Width="150px"></asp:TextBox>

            </asp:Panel>



    </div>

<div class="page">

    <div class="main">
        <asp:ContentPlaceHolder ID="MainContent" runat="server"/>
    </div>
    <div class="clear">
    </div>
</div>

<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" CombineScripts="true" ScriptMode="Release">
</cc1:ToolkitScriptManager>
</form>

要查看的控件是TextBox1 ImageButton1ImageButton2ImageButton1应该是按下输入时触发的唯一按钮,至少在文本框具有焦点时。< / p>

以下是ImageButton1_Click

的代码
 Protected Sub ImageButton1_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click


    Dim Conn As New SqlConnection(ConfigurationManager.ConnectionStrings("Sprayer_Parts_CatalogConnectionString").ConnectionString.ToString)
    Dim sql As New SqlDataSource
    Dim sql3 As New SqlCommand
    Dim sql4 As New SqlCommand
    Dim str2 As String
    Dim str As String
    sql4.Connection = Conn
    sql3.Connection = Conn
    Conn.Open()

    sql3.Parameters.AddWithValue("@Search", TextBox1.Text)
    sql4.Parameters.AddWithValue("@Search", TextBox1.Text)
    sql3.CommandText = "Select Category From SubCategory WHERE Category Like '%' + @Search + '%'"
    sql4.CommandText = "Select Type From SubCategory WHERE Type LIKE '%' + @Search + '%' OR Category LIKE '%' + @Search + '%' OR Order# LIKE '%' + @Search + '%'"
    If Not sql4.ExecuteReader().HasRows Then
        Response.Redirect("~/Sprayer_Parts.aspx?Search=" + "1")

    End If


    Response.Redirect("~/Sprayer_Parts.aspx?Search=" + TextBox1.Text)
    Conn.Close()

End Sub

如果实际点击按钮,一切正常。

ImageButton2_Click

Protected Sub ImageButton2_Click(sender As Object, e As System.Web.UI.ImageClickEventArgs) Handles ImageButton2.Click
    Response.Redirect("~/jcb.aspx")
End Sub

1 个答案:

答案 0 :(得分:1)

如果所有的JCB按钮都在链接到另一个页面,我想理想的是你会把它变成一个带有链接的直接图像,这样搜索按钮就是第一个/唯一一个按钮。

这可能不是最好的方法,但在类似的先前情况下,我截获了返回按键。例如,将此脚本添加到您的页面:

<script>
    function detectSearchBoxReturn(e) {
        var code;
        if (!e) { var e = window.event; }
        if (e.keyCode) { code = e.keyCode; }
        else if (e.which) { code = e.which; }

        if (code == 13) {
            // find your button and perform a click event.
        }
    }
</script>

然后在您的文本框中添加

onkeypress = "detectSearchBoxReturn(event);"

显然,如果禁用脚本,这不起作用,如前所述,这可能不是最好的方法。