Asked by:
Gridview Select Button Problem

Question
-
User-1314346660 posted
Hello Experts,
I have a gridview and a formview. The Gridview has some data and includes the 'ShowSelect' button. When the user clicks on the select button then the formview displays the full dataset for that record. It works great.
The problem is that the Gridview often has lots of records. So the user clicks on the 'ShowSelect' and then has to scroll down to the bottom of the gridview to see the formview.
Is there a way to ensure that when the user clicks the 'ShowSelect' button then this moves the cursor or page view to the formview below??
Thanks as always to the ASP masters for any help!
Billson3000
Friday, November 22, 2019 12:34 PM
All replies
-
User409696431 posted
SInce you didn't post any code, I have to make assumptions, but in your function handling ShowSelect, try setting the focus to the ID of the FormView.
MyFormViewId.Focus();
Monday, November 25, 2019 2:04 AM -
User1535942433 posted
Hi Billson3000,
Accroding to your description,I suggest you could try to add RowDataBound event to trigger the SetScrollEvent(). The SetScrollEvent() function is to click the button then move to buttom of page.
More details ,you could refer to below code:
Solution1:
ASPX:
<script type="text/javascript"> function SetScrollEvent() { window.scroll(0, document.body.scrollHeight); } </script> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound"> <Columns> <asp:BoundField DataField="First" HeaderText="First" /> <asp:BoundField DataField="Third" HeaderText="Third" /> <asp:TemplateField> <ItemTemplate> <asp:Button Text="ShowSelect" CommandName="Select" runat="server" ID="button1"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <u>Selected Row:</u> <br /> <br /> <asp:FormView ID="FormView1" runat="server"> <ItemTemplate> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td> ID: </td> <td> <%# Eval("First") %> </td> </tr> <tr> <td> Name: </td> <td> <%# Eval("Third") %> </td> </tr> </table> </ItemTemplate> </asp:FormView>
Code-behind:
protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Sum"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Sum"); conn.Close(); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); conn.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { bind(); } } protected void OnSelectedIndexChanged(object sender, EventArgs e) { Page.ClientScript.RegisterStartupScript(GetType(), "myscript","<script>window.scroll(0,150)</script>"); string first = GridView1.SelectedRow.Cells[0].Text; string third = GridView1.SelectedRow.Cells[1].Text; DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[2] { new DataColumn("First", typeof(int)), new DataColumn("Third", typeof(int)) }); dt.Rows.Add(first, third); FormView1.DataSource = dt; FormView1.DataBind(); } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType.Equals(DataControlRowType.DataRow)) { foreach (DataControlFieldCell cell in e.Row.Cells) { foreach (Control control in cell.Controls) { Button lb = control as Button; if (lb != null) lb.Attributes.Add("onclick", "SetScrollEvent();"); } } } }
Solution2:
Or you could add a class in the button:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="First" HeaderText="First" /> <asp:BoundField DataField="Third" HeaderText="Third" /> <asp:TemplateField> <ItemTemplate> <asp:Button Text="ShowSelect" CommandName="Select" runat="server" ID="button1" OnClientClick="SetScrollEvent()"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
Result:
Best regards,
Yijing Sun
Monday, November 25, 2019 6:47 AM -
User-1314346660 posted
Hi Kathy,
Thanks this is what is have so far!
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { //Determine the RowIndex of the Row whose Button was clicked. int rowIndex = Convert.ToInt32(e.CommandArgument); //Reference the GridView Row. GridViewRow row = GridView1.Rows[rowIndex]; //Page.SetFocus(FormView1.FindControl("RecordId")); FormView1.Focus(); } }
This works. But...……..It goes to the middle of the FormView. I am trying to get the Focus to a specific control (FirstNameTextBox) in the FormView1.
I am very grateful for any help. Apologies for not posting my existing code.
Monday, November 25, 2019 10:52 AM -
User409696431 posted
Use the control ID of that textbox instead of the formview's id. Does that do what you want?
Monday, November 25, 2019 4:55 PM -
User-1314346660 posted
Hi Kathy,
That's the bit I cannot work out how to do!! I don't know how to reference that control. I have tried:
FormView1.FindControl("FirstNameTextBox").Focus();
But that doesn't do it! - I get an Object reference not set to an instance of an object. error
Monday, November 25, 2019 5:13 PM -
User-1314346660 posted
Arrrrgghhhh. Still not there. This feels closer!
protected void FormView1_DataBound(object sender, EventArgs e) { if (FormView1.CurrentMode == FormViewMode.Edit) { Label lb = (Label)FormView1.Row.Cells[0].FindControl("FirstNameTextBox"); lb.Focus(); } }
Monday, November 25, 2019 7:56 PM -
User409696431 posted
Look at the source in the browser. What is the ID of the FirstNameTextBox?
Tuesday, November 26, 2019 6:11 PM -
User-1314346660 posted
Sorry Kathy. Not sure what you mean??
<asp:Textbox ID="FirstNameTextBox" runat="server" Text='<%# Bind("FirstName") %>' />
I have also tried...
protected void FormView1_DataBound(object sender, EventArgs e) { if (FormView1.CurrentMode == FormViewMode.Edit) { TextBox tb = (TextBox)FormView1.Row.Cells[0].FindControl("FirstNameTextBox"); tb.Focus(); } }
Tuesday, November 26, 2019 8:45 PM -
User1535942433 posted
Hi Billson3000,
Accroding to your description,I suggest you could break point in the sentence of CurrentMode is Edit. Debug and check wheather to perform focus().
I'm guessing that your mode is Readonly,so couldn't run focus().
If you still have question,please post aspx code,it will more faster for us to reproduce your issue and find out the solution.More details ,you could refer to below code:
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnSelectedIndexChanged="OnSelectedIndexChanged"> <Columns> <asp:BoundField DataField="First" HeaderText="First" /> <asp:BoundField DataField="Third" HeaderText="Third" /> <asp:TemplateField> <ItemTemplate> <asp:Button Text="ShowSelect" CommandName="Select" runat="server" ID="button1" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <br /> <u>Selected Row:</u> <br /> <br /> <asp:FormView ID="FormView1" runat="server" OnDataBound="FormView1_DataBound" OnModeChanging="FormView1_ModeChanging"> <ItemTemplate> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td>ID: </td> <td> <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Eval("First") %>'></asp:TextBox> </td> </tr> <tr> <td>Name: </td> <td> <%# Eval("Third") %> </td> </tr> </table> </ItemTemplate> <EditItemTemplate> <table border="0" cellpadding="0" cellspacing="0"> <tr> <td>ID: </td> <td> <asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Eval("First") %>'></asp:TextBox> </td> </tr> <tr> <td>Name: </td> <td> <%# Eval("Third") %> </td> </tr> </table> </EditItemTemplate> </asp:FormView> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox7" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox5" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </ContentTemplate> </asp:UpdatePanel>
Code-Behind:
protected void bind() { string str, strSql; str = System.Configuration.ConfigurationManager.ConnectionStrings["aspnet-TestApplicationWithDatabase-20190820030542ConnectionString"].ConnectionString; SqlConnection conn = new SqlConnection(str); strSql = "select * from Sum"; SqlDataAdapter da = new SqlDataAdapter(strSql, str); DataSet ds = new DataSet(); da.Fill(ds, "Sum"); conn.Close(); this.GridView1.DataSource = ds.Tables[0].DefaultView; this.GridView1.DataBind(); conn.Close(); } protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { bind(); } } protected void OnSelectedIndexChanged(object sender, EventArgs e) { //Page.ClientScript.RegisterStartupScript(GetType(), "myscript","<script>window.scroll(0,150)</script>"); string first = GridView1.SelectedRow.Cells[0].Text; string third = GridView1.SelectedRow.Cells[1].Text; DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[2] { new DataColumn("First", typeof(string)), new DataColumn("Third", typeof(string)) }); dt.Rows.Add(first, third); FormView1.DataSource = dt; FormView1.DataBind(); } protected void FormView1_DataBound(object sender, EventArgs e) { FormView1.ChangeMode(FormViewMode.Edit); if (FormView1.CurrentMode == FormViewMode.Edit) { TextBox tb = (TextBox)FormView1.FindControl("FirstNameTextBox"); tb.Focus(); } }
Result:
Best regards,
Yijing Sun
Wednesday, November 27, 2019 9:38 AM