Have to press search button twice??? - How annoying...

Hia, info coming from database and being displayed in a datagrid according to search criteria when the search button is clicked.

There's no major problem just an annoying one really, the search button has to be pressed twice before any data is displyed. Is there any obvious reason for this?


Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

DataGrid1.Visible = True


If idtext.Text <> "" Then 'If an ID has been entered

SqlDataAdapter1.SelectCommand.CommandText = "SELECT rfqID, rfqQuestion, rfqAnswer, rfqDateAdded,rfqStatus FROM faqlog WHERE rfqID= @ID"

SqlDataAdapter1.SelectCommand.Parameters.Add(New SqlParameter("@ID", SqlDbType.Int, 4)).Value = idtext.Text
SqlDataAdapter1.Fill(DS, "faqlog")

DataGrid1.DataSource = DS.Tables("faqlog").DefaultView


DataGrid1.DataBind()


ElseIf keywordstext.Text <> "" Then 'If a keyword was entered

SqlDataAdapter1.SelectCommand.CommandText = "SELECT rfqID, rfqQuestion, rfqAnswer, rfqDateAdded, rfqStatus FROM faqlog Where rfqkeywords LIKE '%" & keywordstext.Text & "%'"

SqlDataAdapter1.SelectCommand.Parameters.Add(New SqlParameter("@keyword", SqlDbType.VarChar, 100)).Value = keywordstext.Text

SqlDataAdapter1.Fill(DS, "faqlog")

DataGrid1.DataSource = DS.Tables("faqlog").DefaultView

DataGrid1.DataBind()

ElseIf questionddl.SelectedValue <> "Category" Then 'If a category was chosen

SqlDataAdapter1.SelectCommand.CommandText = "SELECT rfqID, rfqQuestion, rfqAnswer, rfqDateAdded, rfqStatus FROM faqlog WHERE rfqQuestion= @question"

SqlDataAdapter1.SelectCommand.Parameters.Add(New SqlParameter("@question", SqlDbType.VarChar, 100)).Value = questionddl.SelectedValue

SqlDataAdapter1.Fill(DS, "faqlog")

DataGrid1.DataSource = DS.Tables("faqlog").DefaultView

DataGrid1.DataBind()

End If

Cheers for the help guysOk I think the prob is this:

When the data is entered, whatever happens next calls the textchanged event handler to come into play and THEN when the search button is clicked the search is carried out.

If the search button is clicked right away, the textchanged event starts to work, so it then needs to be clicked AGAIN for the search to be carried out.

Perhaps I can call the search_click event at the end of each change event???

How do I do this???Sounds to me like when your textbox looses focus, the form is submitted and when you try to click the search button nothing happens because your browser is in the middle of a transaction.

I'd recommend that you set the textbox autopostback value to false, that way the form isn't submitted each time it looses focus. Then hookup some javascript to the onkeypress event of the textbox to catch the enter key and submit the search button.

Here is a JS function I use to cause and enter key press on a textbox to submit a button.

function fnTrapKD(btn, event){
var btn = getObject(btn);
if (document.all){if (event.keyCode == 13){event.returnValue=false;event.cancel = true;btn.click();}}
else{if (event.which == 13){event.returnValue=false;event.cancelBubble = true;btn.click();}}
}

Use it like this:

<input type="text" id="txtSearch" onkeypress="fnTrapKD('Button1',event)" />


Below is how you hookup multiple events to one subroutine (not my recommendation):

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Textbox1.TextChangedHia, I get an error message now. it is saying that idtext (which you called txtsearch) is not declared. It can't find it as it's in an input tag now, whereas it was just a textbox (eg: <asp:textbox>) before???Yeah, sorry bout that, it should be like this:

<input type="text" id="txtSearch" runat="server" onkeypress="fnTrapKD('Button1',event)" />


If you are instantiating this control in a code behind it would be:

protected withevents txtSearch As System.Web.UI.HtmlControls.HtmlInputText


Hope that helps.
 
Back
Top