views:

785

answers:

4

Hi,

I have the following Gridview (which works) and an 'EmptyDataText' tag that should show a message on the screen if the data could not be found from the database after clicking the submit button. The problem is, the message 'no data found' is displayed on the page the moment the page is opened but I want the message to appear only if the user enters an email address which could not be found in the db. I hope I explained it correct, I can't really word this problem in a better way, pleae help!

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                DataSourceID="SqlDataSource" EmptyDataText="The data could not be found" CellSpacing="3" CellPadding="4"
                GridLines="None" ForeColor="#333333">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True"
                        SortExpression="ID">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="EmailAddress" HeaderText="Email Address" SortExpression="EmailAddress">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Address1" HeaderText="Address1" SortExpression="Address1">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Address2" HeaderText="Address2" SortExpression="Address2">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="city" HeaderText="City" SortExpression="city">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                    <asp:BoundField DataField="PostCode" HeaderText="Post Code" SortExpression="PostCode">
                        <ItemStyle Font-Names="Verdana" Font-Size="9pt" />
                        <HeaderStyle Font-Names="Verdana" Font-Size="10pt" />
                    </asp:BoundField>
                </Columns>

                <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                <FooterStyle BackColor="#5D7B9D" ForeColor="White" Font-Bold="True" />
                <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                <EditRowStyle BackColor="#999999" />
            </asp:GridView>

This might be something quite simple, maybe something to do with the way the page refreshes? Or it might be quite difficult... I just don't really want the message to be displayed on the screen even befroe the user has enetered any info.

Thanks.

A: 

Where you ask user to input email address? you can remove the emptydatatext and use the emptyDatatemplate.

<EmptyDataTemplate>
   <table border="1px" cellpadding="1px" cellspacing="1" width='100%'>
            <tr><td>...   </td> </tr>
   </table>
</EmptyDataTemplate>
Henry Gao
you cna also make the Gridview invisible first, then whatever event fire to make it visible
Henry Gao
A: 

Hi,

The text input part is:

<form id="form1" runat="server">
        <div>
            <div style="margin-bottom: 15px">
                <strong><span style="font-family: Verdana"><span style="font-size: 10pt">Enter email
                    address:</span></span></strong></div>
            <div style="margin-bottom: 15px">
                <asp:TextBox ID="txtBox" runat="server" Width="210px" TabIndex="1" MaxLength="50"></asp:TextBox></div>
            <asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" Width="93px" />
            <div style="margin-top: 15px">
                <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtBox"
                    Text="Please enter a valid email address" runat="server" Font-Bold="False" Font-Names="Verdana"
                    Font-Size="10pt" />
            </div>
            <div style="margin-top: 30px; margin-bottom: 15px">
            </div>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID"
                DataSourceID="SqlDataSource" EmptyDataText="wrong data" CellSpacing="3" CellPadding="4"
                GridLines="None" ForeColor="#333333">
Mike
Making the Gridview invisible at the beginning will work in this case.
Henry Gao
Hi Henry, is that pretty easy to do (I'm quite new to this)? :)
Mike
Yeah, you can add visible="false" inside GridView. add gridview1.visible = true in method of btn_click
Henry Gao
Thank you so much mate, that works a treat!
Mike
You wouldn't possibly know the following question that has been annoying me all day would you... I have an EmptyDataText="no info" tag that is shown after the email address has been entered and no info could be found from it but what I am wanting is to style it so it is centre of page rather then the left. I have a <EmptyDataRowStyle Font-Size="12px" Font-Names="verdana" /> which works but then when I try and align the text apparently it's not a valid attribute. Please help!
Mike
did you try add cssClass to <EmptyDataRowStyle cssclass="mycentertext" />?
Henry Gao
I haven't until now. Do I add the following in my style.css page: mycentertext{ text-align: center;}
Mike
Update: in the css file, I had to put a . in front so it become .mycentertext but the text-align: center; does not work yet the font style and sixe does. .mycentertext{ text-align: center; font-size: 12px; font-family: Verdana;}
Mike
Do you need align center inside GridView or the whole screen?
Henry Gao
A: 

if you have

when you view the source from browser, you will get

<div>
    <table cellspacing="0" rules="all" border="1" id="GridView1" style="border-collapse:collapse;">
     <tr class="mytextcenter">
      <td>The data could not be found</td>
     </tr>
    </table>
</div>

you can easily to figure out what css you need to specify.

Henry Gao
A: 

I'm wanting to align centre (in the gridview) the EmptyDataText="no info" tag rather than it being on the default left of the page.

Mike