on button click form moves to the top

  • Thread starter Thread starter gjtired
  • Start date Start date
G

gjtired

Hi,

When I click a button or dropdown box at the bottom of the web form the
focus shifts to the top of the form. What can I do to make it stay
where it is at?

Thanks

Gayle
 
I think you have to check for Page.IsPostBack in the Page_Load function. So
something like:

private void Page_Load(object sender, System.EventsArgs e)
{
if (Page.IsPostBack == false)
{
// Code goes here.
}
}

That'll keep the page from reloading everytime you change a selection in the
dropdown box.
 
You can turn on SmartNavigation, although under certain circumstances that
can prove problematic. Here's how I take care of it in ASP.NET 1.1 (in
ASP.NET 2.0, you have special overloads that don't require separate code for
this issue):

// in Global.asax.cs:

public class Global : System.Web.HttpApplication
{

// insert static SetFocus method here, just below the class Global
declaration:
public static void SetFocus(System.Web.UI.Page webPage)
{
string[] pbCtrl = webPage.Page.Request.Form.GetValues("__EVENTTARGET");
if (pbCtrl != null && pbCtrl.Length > 0)
{
string ctrlId;
ctrlId = pbCtrl[0];
System.Web.UI.Control ctrlFound = webPage.Page.FindControl(ctrlId);
if ((ctrlFound != null) &&
(
ctrlFound is System.Web.UI.WebControls.DropDownList ||
ctrlFound is System.Web.UI.WebControls.TextBox ||
ctrlFound is System.Web.UI.WebControls.RadioButton ||
ctrlFound is System.Web.UI.WebControls.RadioButtonList))
{
string ctrlClientId;
ctrlClientId = ctrlFound.ClientID;
string strScript;
strScript = "<SCRIPT language=\"javascript\"> document.getElementById('" +
ctrlClientId + "').focus(); document.getElementById('"
+ ctrlClientId + "').scrollIntoView(true) </SCRIPT>";
webPage.Page.RegisterStartupScript("controlFocus",strScript );
}
}
}

// In your Page_Load handler for (any page:
private void Page_Load(object sender, System.EventArgs e)
{
// insert this conditional call to the SetFocus Method:
if(IsPostBack) Global.SetFocus(this);
 
I had a if (!IsPostBack)
{
//code
}

I tried it your way and it still does the same thing. Below is what I
have.

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{
// copies Task and TaskType index from TaskToPerform.aspx
txtTaskType.Text=Request.QueryString["TaskType"];
txtTask.Text=Request.QueryString["Task"];

//Fill drpQuestionType
sqlConnection1.Open();
daQuestionType.Fill(dsMain1.QuestionType);
DataTable dtQuestionType = dsMain1.Tables["QuestionType"];


//Make type of question invisible if editing a question
//lblTypeQuestion.Visible = false;
drpTypeQuest.Visible = false;

//fills the drpTypeQuest
foreach (DataRow dataRow in dtQuestionType.Rows)
{

ListItem questtype= new ListItem();

questtype.Text=dataRow["QuestionType_Option"].ToString().Trim();
questtype.Value=dataRow["QuestionType_ID"].ToString().Trim();
drpTypeQuest.Items.Add(questtype);
} //end foreach loop

string sqlEdit="SELECT * " +
"FROM BTForm " +
"WHERE BTForm_Status = 'Active' " +
"Order By BTForm_Name";

SqlDataAdapter daEdit=new SqlDataAdapter(sqlEdit, sqlConnection1);
dsMain1.BTForm.Clear();
daEdit.Fill(dsMain1.BTForm);
DataTable BTFormDT = dsMain1.Tables["BTForm"];

//fills the drpSelectForm
foreach (DataRow BTFormRow in BTFormDT.Rows)
{

ListItem newBTForm= new ListItem();

newBTForm.Text=BTFormRow["BTForm_Name"].ToString().Trim();
newBTForm.Value=BTFormRow["BTForm_ID"].ToString().Trim();
drpSelectForm.Items.Add(newBTForm);
} //end foreach loop


sqlConnection1.Close();
}//end ispostback if
}//Page_Load
 
Back
Top