Using a repeater to display search results and then redirecting to another form

tlox 0 Tallied Votes 2K Views Share

Hello everyone. i have a student database, from which i search a student by entering the student ID at the web interface.Im using a repeater to display search results. Within this repeater i have a link button that should redirect to a page that will display full information about the student.

My problem is that im failing to display the full details of the students.Can someone help or at least suggest a better way of doing this because i doubt if the repeater is the best solution.

Thanks!

part of the .aspx code

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT Stud_ID, Stud_Name, FileNumber FROM ActiveFiles WHERE (Stud_ID LIKE Stud_ID) OR (Stud_Name LIKE Stud_Name) ORDER BY FileNumber">
</asp:SqlDataSource>

<asp:Repeater ID="Repeater1" runat="server" onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
    <hr />
        <%# DataBinder.Eval(Container.DataItem, "Stud_ID") %>
    <tb>
      <%# DataBinder.Eval(Container.DataItem, "Stud_Name") %>
    <tb> 
     <asp:LinkButton ID="LinkButton1" CommandName="FileNo" OnClick="LinkButton1_Click"
     Text=<%# DataBinder.Eval(Container.DataItem, "FileNumber") %> runat="server"/>       
    </tb>
   <hr />
</ItemTemplate>
...
</asp:Repeater>


part of the .cs code


protected void SearchButton_Click(object sender, EventArgs e)
{
        string ID_OR_name = TextBox1.Text;

        if (TextBox1.Text != string.Empty)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();

            SqlCommand selectStatement = new SqlCommand("SELECT * FROM ActiveFiles WHERE (Stud_ID               LIKE '%  " + ID_OR_name + "%') OR (Stud_Name LIKE '%" + ID_OR_name + "%') ORDER BY                   FileNumber", conn);
                        
            SqlDataReader datTable = selectStatement.ExecuteReader();
            Repeater1.DataSource = datTable;
            Repeater1.DataBind();

            conn.Close();

            GridView2.DataBind();
            GridView2.Visible = true;
            Response.Write("Search query produced this results");
        }
        else
            Response.Write("Plis enter ID or name to search");
}
 
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{     
        if (e.CommandName == "FileNo")
        {           
            Response.Write("good");
            //redirect to the page that displays the searched student's detail
            //this is where im stuck
        }
}
kvprajapati 1,826 Posting Genius Team Colleague

Suggestions:

1. No need to use SqlDataSource. Have a look at SearchButton's click. You are populating repeater manually.

2. Never use hard-coded sql strings especially with select statement.

3. You may use CommandArgument property to send button or record specific info to the ItemCommand handler.

kouroshnik 0 Light Poster

page.aspx:

<asp:LinkButton 
     ID="btnPage" 
     runat="server" 
     CommandName="FileNo" 
     CommandArgument='<%# DataBinder.Eval(Container.DataItem, "FileNumber") %>' 
     Text='<%#DataBinder.Eval(Container.DataItem, "FileNumber") %>' >
 </asp:LinkButton>

page.aspx.cs:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
  {             
    if (e.CommandName == "FileNo")        
     { 
        string ID = e.CommandArgument.toString();                   
        Response.Redirect("page.aspx?id="+id);
     }
  }
tlox 0 Newbie Poster

Thank you guys. Your suggestions helped me out. I have two text boxes in a page where the student details will be displayed. textboxID to display student ID, and textboxName to display student name. But upon clicking the linkbutton, the student ID and name are all displayed in the first textbox.How can i "redirect" these two to different textboxes which are in the same page? I know very little about state management options in asp.net, but im just starting to explore them.
Thanks.

page1.aspx

<asp:LinkButton ID="LinkButton2" runat="server" 
CommandName="StudentID"  
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "studID") + "," +                                       DataBinder.Eval(Container.DataItem, "studName") %>' Text='<%#DataBinder.Eval(Container.DataItem, "studID") + "," + DataBinder.Eval(Container.DataItem, "studName") %>' > 
</asp:LinkButton>

page1.aspx.cs

protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e)
{  
      
        if(e.CommandName == "StudentID")
        {            
            string studentid = e.CommandArgument.ToString();
            //string studentname = e.CommandArgument.ToString();
            Response.Redirect("Page2.aspx?id=" + studentid);
         /*Response.Redirect("Page2.aspx?id={0}&name={1}&file={2}" + studentid + studentname);<---tried this*/
        }

}

page2.aspx

<asp:Label ID="Label1" runat="server" Text="Search Results are as thus:"></asp:Label>
        <br />
        <br />
        <asp:Label ID="Label2" runat="server" Text="Student ID"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
        <br />
        <br />
        <asp:Label ID="Label3" runat="server" Text="Student Name"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>

page2.aspx.cs

public partial class SearchResults : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, EventArgs e)
    {    
        if ((Request.QueryString["id"] != null) || (Request.QueryString["name"] != null))
        {
                       
            TextBox1.Text = Request.QueryString["id"];
            TextBox2.Text = Request.QueryString["name"];
            ...
        }
        
        else
            Response.Write("xxxxx");
    }
}
asemoonabi 0 Newbie Poster

Response.Redirect("Page2.aspx?id={0}&name={1}&file={2}" + studentid + studentname);<---tried this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.