Answered by:
Gridview - C# - asp page (with master) - Grid remains on Screen AFTER select

Question
-
User-973788521 posted
Hello, I have an issue that I need your help with please. I have a ASP page (C#) with a Gridview pulling data from SQL Server (code below). This one, when I press select (
<asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" />
), I get the value to the textbox and the gridview disappears. That is what I want. I have two other gridviews on the same page, but on select, they are leaving the gridviews ON the screen - not the entire gridview return, just the selected item (i.e. if search yields 15 results, gridview show 15 results. When I select 1 of the results, gridview remains on screen, but only the one record selected.). I dont want that. What can I do to tell the gridview that after select, go away...
<%-- ReSharper disable Asp.Image --%> <asp:ImageButton ID="imgPatSearch" ImageUrl="../../images/validate2.jpg" runat="server" Height="30px" Width="30px" OnClick="BindData" /> <br /> <%-- ReSharper restore Asp.Image --%> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="White" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PatientName" HeaderText="Patient Name" SortExpression="PatientName" /> <asp:BoundField DataField="PatientId" HeaderText="Patient Id" SortExpression="PatientId" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ABC %>" SelectCommand="GetPatientData" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="txtFirstname" Name="p1" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource>
Full Code: (well, two of the three, but you get the gist. I am basically copying and pasting the values.)
<%-- ReSharper disable Asp.Image --%> <asp:ImageButton ID="imgPatSearch" ImageUrl="../../images/validate2.jpg" runat="server" Height="30px" Width="30px" OnClick="BindData" /> <br /> <%-- ReSharper restore Asp.Image --%> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource1" ForeColor="White" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Select" ID="lnkSelect" runat="server" CommandName="Select" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="PatientName" HeaderText="Patient Name" SortExpression="PatientName" /> <asp:BoundField DataField="PatientId" HeaderText="Patient Id" SortExpression="PatientId" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ABC %>" SelectCommand="GetPatientData" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="txtFirstname" Name="p1" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource> <br /> <asp:Label ID="Label5" runat="server" Text="Nurses Name / ID: "></asp:Label> <asp:TextBox ID="txtNurseName" runat="server" Width="200px" TextMode="SingleLine"></asp:TextBox> <asp:TextBox ID="txtNurseId" runat="server" TextMode="SingleLine"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvNurseId" runat="server" ErrorMessage="Please provide your Employee ID." ControlToValidate="txtNurseId" Display="None" ValidationGroup="nidCheck" ForeColor="Red" /> <ajaxToolkit:ValidatorCalloutExtender ID="ValidatorCalloutExtender4" runat="server" TargetControlID="rfvNurseId" /> <%-- ReSharper disable Asp.Image --%> <asp:ImageButton ID="imgRnSearch" ImageUrl="../../images/validate2.jpg" runat="server" Height="30px" Width="30px" OnClick="BindData3" /> <br /> <%-- ReSharper restore Asp.Image --%> <asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource3" ForeColor="White" GridLines="Vertical" OnSelectedIndexChanged="GridView3_SelectedIndexChanged"> <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> <Columns> <asp:TemplateField> <ItemTemplate> <asp:LinkButton Text="Select" ID="lnkSelect3" runat="server" CommandName="Select" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="StaffId" HeaderText="Staff ID" SortExpression="StaffId" /> <asp:BoundField DataField="StaffName" HeaderText="Staff Name" SortExpression="StaffName" /> </Columns> <EditRowStyle BackColor="#999999" /> <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> <SortedAscendingCellStyle BackColor="#E9E7E2" /> <SortedAscendingHeaderStyle BackColor="#506C8C" /> <SortedDescendingCellStyle BackColor="#FFFDF8" /> <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ABC %>" SelectCommand="GetStaffId" SelectCommandType="StoredProcedure"> <SelectParameters> <asp:ControlParameter ControlID="txtNurseName" Name="p3" PropertyName="Text" Type="String" /> </SelectParameters> </asp:SqlDataSource>
Saturday, December 3, 2016 8:03 PM
Answers
-
User-707554951 posted
Hi doctork32,
From your description and code, I suggest you could hid GridView from code behind. Following is an example, you could refer to it:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CategoryID" AllowSorting="true" AutoGenerateSelectButton="true" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("CategoryID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="CategoryName" DataField="CategoryName" SortExpression="CategoryName"/> </Columns> <SelectedRowStyle BackColor="#ffffcc"/> </asp:GridView>
CodeBehind:
using System.Data.SqlClient; using System.Data; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGridView(); } } private void BindGridView() { string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (System.Data.SqlClient.SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from Categories")) { using (System.Data.SqlClient.SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); if (ds.Tables.Count > 0) { System.Data.DataView dv = ds.Tables[0].DefaultView; string sortExpression = ""; string sortDirection = ""; if (ViewState["SortDirection"] != null) { sortDirection = ViewState["SortDirection"].ToString(); } if (ViewState["SortExpression"] != null) { sortExpression = ViewState["SortExpression"].ToString(); dv.Sort = string.Concat(sortExpression, " ", sortDirection); } GridView1.DataSource = dv; GridView1.DataBind(); } } } } } } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { } protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { GridView1.SelectedIndex = e.NewSelectedIndex; BindGridView(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gwr = GridView1.SelectedRow; string name = gwr.Cells[3].Text.ToString(); Label lb = (Label)gwr.FindControl("Label1"); TextBox1.Text = lb.Text; TextBox1.Text += name; GridView1.Style["display"] = "none"; }
Output:
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 5, 2016 2:51 AM
All replies
-
User-973788521 posted
C# Code, just in case you ask
protected void BindData(object sender, ImageClickEventArgs imageClickEventArgs) { GridView1.DataBind(); } protected void BindData2(object sender, ImageClickEventArgs imageClickEventArgs) { GridView2.DataBind(); } protected void BindData3(object sender, ImageClickEventArgs imageClickEventArgs) { GridView3.DataBind(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { txtFirstname.Text = GridView1.SelectedRow.Cells[1].Text; txtPatId.Text = GridView1.SelectedRow.Cells[2].Text; } protected void GridView2_SelectedIndexChanged(object sender, EventArgs e) { txtPhysName.Text = GridView2.SelectedRow.Cells[1].Text; } protected void GridView3_SelectedIndexChanged(object sender, EventArgs e) { txtNurseId.Text = GridView3.SelectedRow.Cells[1].Text; txtNurseName.Text = GridView3.SelectedRow.Cells[2].Text; }
Saturday, December 3, 2016 8:05 PM -
User-707554951 posted
Hi doctork32,
From your description and code, I suggest you could hid GridView from code behind. Following is an example, you could refer to it:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" DataKeyNames="CategoryID" AllowSorting="true" AutoGenerateSelectButton="true" OnSelectedIndexChanging="GridView1_SelectedIndexChanging" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" AutoPostBack="true"/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%#Eval("CategoryID")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="CategoryName" DataField="CategoryName" SortExpression="CategoryName"/> </Columns> <SelectedRowStyle BackColor="#ffffcc"/> </asp:GridView>
CodeBehind:
using System.Data.SqlClient; using System.Data; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGridView(); } } private void BindGridView() { string constr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; using (System.Data.SqlClient.SqlConnection con = new SqlConnection(constr)) { using (SqlCommand cmd = new SqlCommand("select * from Categories")) { using (System.Data.SqlClient.SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet ds = new DataSet()) { sda.Fill(ds); if (ds.Tables.Count > 0) { System.Data.DataView dv = ds.Tables[0].DefaultView; string sortExpression = ""; string sortDirection = ""; if (ViewState["SortDirection"] != null) { sortDirection = ViewState["SortDirection"].ToString(); } if (ViewState["SortExpression"] != null) { sortExpression = ViewState["SortExpression"].ToString(); dv.Sort = string.Concat(sortExpression, " ", sortDirection); } GridView1.DataSource = dv; GridView1.DataBind(); } } } } } } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { } protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { GridView1.SelectedIndex = e.NewSelectedIndex; BindGridView(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { GridViewRow gwr = GridView1.SelectedRow; string name = gwr.Cells[3].Text.ToString(); Label lb = (Label)gwr.FindControl("Label1"); TextBox1.Text = lb.Text; TextBox1.Text += name; GridView1.Style["display"] = "none"; }
Output:
Best regards
Cathy
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, December 5, 2016 2:51 AM