Hide Shown Items in GridView based on stored procedure in ASP.Net

nedash
 
on Mar 11, 2013 04:34 AM
2570 Views

Hi

I have 2 page admin.aspx page  and search.aspx

in admin .aspx page I have textbox=txtsearch and image button=imgs

in txtsearch we can type Mobile Or phoneNumber and when we click on Imgs we go to search.aspx page

below are code

 

 

  protected void imgs_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("admin/search.aspx?Mobile=" + Server.UrlEncode(Txtsearch.Text));
    }

 

in search.aspx page I have gridview that bind from database and show data according to Mobile or PhoneNumber that I typed in txtsearch in admin.aspx below are code

 

 

   <ContentTemplate>
       <asp:GridView ID="GridView1" runat="server" CssClass="DGridViewSV"
        AutoGenerateColumns = "false" Font-Names = "Tahoma" 
        Font-Size = "9pt" 
        AllowPaging ="true"
        OnPageIndexChanging = "OnPaging"  Width="485px"

        PageSize = "6"  GridLines ="Horizontal" RowStyle-BorderColor="#cecece" RowStyle-BorderStyle="None" PagerStyle-HorizontalAlign="Center" PagerStyle-VerticalAlign="Middle" BorderStyle="None" FooterStyle-BorderStyle="None"  ShowHeader="False" RowStyle-Height="20px">

       <Columns>
       <asp:TemplateField ItemStyle-Width = "15px" >
        
            <ItemTemplate>
                <asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>' Visible="false"></asp:Label>
            </ItemTemplate> 
           
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width = "125px"   ItemStyle-Height="24px">
            <ItemTemplate>
 <asp:Label ID="lblmob" runat="server" Text='<%# Eval("Name")%>' CssClass="lblmobVE" Visible = '<%# !string.IsNullOrEmpty(Eval("Mobile").ToString())%>'></asp:Label>

           

                           

            </ItemTemplate>
           
        </asp:TemplateField>
        <asp:TemplateField ItemStyle-Width = "100px"  ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
               


<asp:HyperLink ID="HLClass" runat="server" CssClass="lblmobVE" NavigateUrl = '<%#"View-users-info.aspx?id="+Eval("id")  %>' Target="_blank"><%#Eval("Mobile") %></asp:HyperLink>
<asp:HyperLink ID="HyperLink1" runat="server" CssClass="lblmobVE" NavigateUrl = '<%#"View-users-info.aspx?id="+Eval("id")  %>' Target="_blank"><%#Eval("Phone") %></asp:HyperLink>


            </ItemTemplate>
        
        </asp:TemplateField>
          <asp:TemplateField ItemStyle-Width = "40px"  ItemStyle-HorizontalAlign="Center">
            <ItemTemplate>
            <asp:Label ID="lblcode" runat="server" Text='<%# Eval("code")%>' CssClass="lblmobVE"></asp:Label>

            </ItemTemplate>
           </asp:TemplateField>

in grid view I have 4 column

1-Id

2-Name

3-Mobile & Phone

4-Code

BIHIND CODE

 

 

 private void BindData()
    {
        string data = Server.UrlDecode(Request.QueryString["Mobile"]);
        SqlCommand cmd = new SqlCommand("Search_number", _cn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Mobile",data); 

        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

Now I want when I enter Mobile in txtsearxh and click button when it go to search.aspx page it in grid view it show Mobile number in HLcalss from database and it doesn't show PhoneNumber and Code in Hyperlink1 and lblCode

and if I type phoneNumber in txtsearch in gridview it showes PhoneNumber and Code and it doesn't Show Mobile number

 

How I can do it?

Thanks alot

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
on Mar 11, 2013 06:11 AM

Ok change you procedure like this

ALTER procedure [dbo].[Search_number]
@Mobile nvarchar(15)
AS
BEGIN
SET NOCOUNT ON

IF EXISTS (SELECT Mobile FROM House_Info WHERE Mobile=@Mobile)
BEGIN
	SELECT  ID,Name,Mobile AS ContactNo,Code,
	FROM
	House_Info
	WHERE Mobile=@Mobile
END
ELSE
BEGIN
    SELECT  ID,Name,Phone AS ContactNo,Code,
	FROM
	House_Info
	WHERE Phone=@Mobile
END
END

Now bind the ContactNo Column with the gridview where you want to display Phone Number or Mobile Number

RkHirpara
 
on Mar 12, 2013 05:51 AM


Hi Nedash,

please alater the SP as below ....

ALTER PROCEDURE [dbo].[Search_number]
	@Mobile NVARCHAR(15)
AS
BEGIN
SET NOCOUNT ON
 
IF EXISTS (SELECT Mobile FROM House_Info WHERE Mobile=@Mobile)
BEGIN
    SELECT 
		ID
		,Name
		,Mobile AS ContactNo
		,'' As Code
    FROM
		House_Info
    WHERE Mobile = @Mobile
END
ELSE
BEGIN
    SELECT 
		ID
		,Name
		,Phone AS ContactNo
		,Code
    FROM
		House_Info
    WHERE Phone=@Mobile
END
END

Thanks and Regards,

Rk_Hirpara.