Have to press Enter Twice for a function to work C# - asp.net

I have the following .aspx page. I'm searching for the words that are in the database, everything works fine. I also have a HyperLink which shows a message if the word does not exist in the database.
BUT the only problem is when I search for the word and the word is not in the database it doesn't show the HyperLink from the first time, I have to CLICK ENTER twice for it to work, and CLICK CLEAR twice for it to dissapear.
I know that the problem is that I have the button after the page_load, but I can't find the solution for it. What I've tried is created another function private void load_data() and copy everything from page_load into it. And in the load_page and button2_click just call load_data(); It doesn't work.
Here's the code:
namespace TRI_Portal.ScreenPop.Gloss
{
public partial class Gloss_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (GridView2.Rows.Count == 0)
{
HyperLink1.Visible = true;
}
else
{
HyperLink1.Visible = false;
}
String s = Request.QueryString["language"];
Language1.Text = Server.HtmlEncode(s);
}
}
}
I have tried this as well, but no luck.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetLink();
}
}
private void SetLink()
{
if (GridView2.Rows.Count == 0)
{
HyperLink1.Visible = true;
}
else
{
HyperLink1.Visible = false;
}
String s = Request.QueryString["language"];
Language1.Text = Server.HtmlEncode(s);
}
protected void Button2_Click(object sender, EventArgs e)
{
SetLink();
}
Any suggestions?

Handle GridView.DataBound event
protected void GridView1_DataBound(object sender, EventArgs e)
{
SetLink();
}
And markup:
<asp:GridView OnDataBound="GridView1_DataBound" ....
Remove the code from the Page_Load event.

Related

PageIndexChanging Event in search button in asp.net?

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid();
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
objRetailPL.ZoneName = ddlzone.SelectedValue;
//IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
//String datetime = txtdate.Text.ToString();
//DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
//objRetailPL.date =dt;
DataTable dtsearch = new DataTable();
dtsearch = objRetailBAL.searchzone(objRetailPL);
GVRate.DataSource = dtsearch;
GVRate.DataBind();
}
protected void GVRate_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GVRate.PageIndex = e.NewPageIndex;
grid();
}
catch (Exception ex)
{
}
}
I have this code for searching records.And for that grid i added page index,but it is not coming properly.When click on search button it is showing more n of records at that time page index is not working. It is calling First grid before clicking search button..How can I change my code please help me.....
Try this ...
private void grid()
{
if(!string.IsNullOrEmpty(yourSearchTextBox.Text)
{
// Then call search method. Make sure you bind the Grid in that method.
}
else
{
// Normally Bind the Grid as you are already doing in this method.
}
}
you have to check whether there is any search Text or not in that grid() method like...

ASP.Net OnLoad Stop event handling

I have some validation code in my WebForm page. When a user clicks on a button and does a postback.
Page_Load event gets processed then the Button1_Click event gets processed.
I can't figure out a way to stop the Button1_Click event from processing if the validation fails on Page_Load.
Is a trick to do this?
Thanks
4 variations shown below.
public partial class _Default : System.Web.UI.Page
{
private bool MyPageIsValid { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
bool valid = false; /* some routine here */
MyPageIsValid = valid;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.MyPageIsValid)
{
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
if (!this.MyPageIsValid) {return;}
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
protected void Button3_Click(object sender, EventArgs e)
{
if (this.Page.IsValid)
{
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
if (!this.Page.IsValid) {return;}
this.TextBox1.Text = DateTime.Now.ToLongTimeString();
}
}
I think it should be better to check validation conditions exactly in the Button1_Click method like this:
if (!this.IsValid()) { return; }
Also if you still want to check that conditions in Page_Load method just add simple 'bool isValid' flag to your page's class and then check it in Button1_Click:
if (!this.isValid) { return; }

Why does DataPager need Pre-Render event to work??

Having to use pre render is causing me problems.. It would be great if I did not need it.. The problem is I have the list in a user control and when I goto the next 'page' I databind.. but then the datapager prerenders.. which also does a batabind.. so it runs twice..
If I remove the prerender .. then clicking next 'page' does nothing..
Any idea?
protected void Page_Load(object sender, EventArgs e)
{
GetSearchResults();
}
//protected void dpMembers_PreRender(object sender, EventArgs e)
//{
// GetSearchResults();
//}
public void GetSearchResults()
{
List<Person> listPerson = new List<Person>();
string strServer = "localhost";
string strAppPath = Server.MapPath("/");
PersonBusiness pb = new PersonBusiness(new PersonRepository());
listPerson = pb.GetAllPersons(strServer, strAppPath);
lvPersons.DataSource = listPerson;
lvPersons.DataBind();
}
Modify your Page load to
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GetSearchResults();
}
}
your prerender seems ok.

How to know the previous event handled in asp.net

on page load i have shown data from database onto a gridview(with edit and delete option). I also have a search button on the page. when i click on search the searched data should be visible in the gridview, which is working fine. But when i click on delete link after search it does not take the searched row. The page postsback after clicking on delete. I need to check the event performed prior to event performed on gridview. How to do that?? I hope that the issue is clear.. Any help is appreciated. Thanks.
Page_Load occurs before RowCommand, are you only binding grid if(!IsPostBack) ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void SearchButton_Click(object sender, EventArgs e)
{
BindGrid(searchText.Text);
}
protected void GridView_RowCommand(object sender, GridViewRowEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
DeleteRow(e.Row.CommandArgument);
break;
}
}
private void BindGrid()
{
//GridView binding business logic.
}
private void BindGrid(String searchTerm)
{
//GridView binding logic with searchTerm.
}
private void DeleteRow(String commandArg)
{
//Convert command arg to ID or whatever and delete data.
}

How to preserve dynamically created controls?

I want to preserve the dynamically created control when postback occurs .
protected void Page_Load(object sender, EventArgs e)
{
}
private void CreateTable()
{
HtmlTableRow objHtmlTblRow = new HtmlTableRow();
HtmlTableCell objHtmlTableCell = new HtmlTableCell();
objHtmlTableCell.Controls.Add(new TextBox());
objHtmlTblRow.Cells.Add(objHtmlTableCell);
mytable.Rows.Add(objHtmlTblRow);
this.SaveControlState();
// this.Controls.Add(mytable);
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateTable();
}
It can be achieved by calling CreateTable() in Page_Load. Is there any alternative way to preserve the control
Thanks
You can add them to a List when you create them and save your List to Session. On postback (Page_Load) load them from your Session to your Page.
the below code should work
protected void Page_PreInit(object sender, EventArgs e)
{
Control myControl = GetPostBackControl(this.Page);
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
CreateTable()
}
public static Control GetPostBackControl(Page thisPage)
{
Control ctrlPostedback = null;
string ctrlName = thisPage.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
{
ctrlPostedback = thisPage.FindControl(ctrlName);
}
else
{
foreach (string Item in thisPage.Request.Form)
{
Control c = thisPage.FindControl(Item);
if (((c) is System.Web.UI.WebControls.Button))
{
ctrlPostedback = c;
}
}
}
return ctrlPostedback;
}
The code works from UpdatePanel
Reference:http://www.asp.net/ajax/videos/how-to-dynamically-add-controls-to-a-web-page

Resources