when I go onto my product search, the text from my EmptyDataTemplate shows up before I have even searched! can anyone shed light on this situation?
<asp:GridView ID="gvProducts" runat="server" CellPadding="4" DataSourceID="sdsProducts" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<SortedAscendingCellStyle BackColor="#FDF5AC" />
<SortedAscendingHeaderStyle BackColor="#4D0000" />
<SortedDescendingCellStyle BackColor="#FCF6C0" />
<SortedDescendingHeaderStyle BackColor="#820000" />
<EmptyDataTemplate>
<asp:Label Text="Sorry, no results found" SkinID="ErrorMessage" runat="server" />
</EmptyDataTemplate>
</asp:GridView>
Thanks.
This is because of how you bind your data source. When you declare a data source like DataSourceID="sdsProducts" in your GridView's markup, that data source will bind its data to your GridView each time the page loads.
So when your page loads for the first time, your data source retrieves an empty set of data and binds it to the GridView, showing your EmptyDataTemplate.
An alternative approach is to bind via the code behind. This way you tell it exactly when you want it to bind, avoiding any binding on the first page load if you so choose.
Related
I'm trying to update GridView every time I update, delete or insert any record. Now I have provided sql data source code for GridView in Design Code not form code. Now how can I update it from there ? When I write GridView1.databind() on form code, it says
> Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Can someone tell me how to DataBind in design view, to update GridView every time I insert/update/delete record ?
Here is GridView1 Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" Width="284px">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="surname" HeaderText="surname" SortExpression="surname" />
<asp:BoundField DataField="amount" HeaderText="amount" SortExpression="amount" />
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myDbConnectionString %>" SelectCommand="SELECT [name], [surname], [amount] FROM [Table1]"></asp:SqlDataSource>
Set the DataSourceID to null.
GridView1.DataSourceID = null;
GridView1.DataSource = dt;
GridView1.DataBind();
That error was coming because data was being binded on both Design view code as well as web form backend code.
I simply added GridView1.DataSourceID = null;
and that did the job.
I get the following error at the time of DataBinding() for the gridview.
Eventhough the "siteName" field is defined as part of the structure "StructSelectedSiteList", the DataBinder(...) in ASP.net gridview definition does not recognize the field. All the fields and the structure are defined as public.
I had referred to previous posts, but it did not help.
DataBinding does not contain a property
Appreciate your response. Thanks!
Here's the code.
StructSelectedSiteList - structure which is populated with values. this is the data source.
[Serializable]
public struct StructSelectedSiteList
{
public string siteName;
public int taskId;
};
In code below, I populate the structure.
var lstSites = new List<StructSelectedSiteList>();
LOOP1:
// populate the struct for selected sites
StructSelectedSiteList structSelectedSiteList;
structSelectedSiteList.siteName = lblSiteName.Text;
structSelectedSiteList.taskId = taskId;
lstSites.Add(structSelectedSiteList);
Now the gridview is bound with populated structure.
gvSelectedSites.DataSource = lstSites;
gvSelectedSites.DataBind(); <<----- Error occurs here
The ASP.net gridview definition is-
<asp:GridView ID="gvSelectedSites" runat="server" AutoGenerateColumns="False" AllowSorting="True"
CellPadding="4" EmptyDataText="There are no data records to display." Font-Names="Segoe UI"
Width="605px" ForeColor="#333333" GridLines="None" RowStyle-HorizontalAlign="Center"
EmptyDataRowStyle-Font-Bold="true" EmptyDataRowStyle-ForeColor="Red" OnRowDataBound="gvSelectedSites_OnRowDataBound">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="siteName">
<ItemTemplate>
<asp:Label ID="lblSiteName" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "siteName")%>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task" Visible="false">
<ItemTemplate>
<asp:Label ID="lblTaskId" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "taskId")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</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>
After testing I found out that I was accessing 'siteName' as structure field, but I need to access it as a property. Hence, after converting all the fields of the structure into properties, I am able to proceed without the exception. Thanks to solutions "DataBinding: 'index+NewsItem' does not contain a property with the name 'Link'", but property exists (Not a Typo) and 'myObject' does not contain a property with the name 'ID' (Not a typo)
I have a gridview on a asp web page and the formatting works fine except when I add AllowPaging or if I set the location of the gridview. Here is the gridview code that I am using.
<asp:GridView
ID="gridview01"
runat="server"
SkinId="Professional"
AllowPaging="true"
CellPadding="4"
EnableModelValidation="True"
ForeColor="#333333"
GridLines="None"
Height="179px"
Width="496px"
style="margin-right: 0px">
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
The top portion is just laid out like that so that I could read it easier. The gridview is populated when a button is pressed. There is code behind the button that calls the query based on the input of two drop down lists.
Use THis Code at BackEnd For Paging in Grid or ListView
protected void Grid_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
GridDataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindGridView();
}
Let's take this code as example:
<div style="overflow:auto;width:700px;">
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AllowSorting="True"
AutoGenerateEditButton="True" DataMember="DefaultView"
DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
DataKeyNames="..." CellPadding="4" ForeColor="#333333" Width="90%"
Height="90%" Font-Size="Small">
<RowStyle BackColor="#EFF3FB" />
<Columns>.
.
.
<asp:CommandField DeleteText="delete" ShowDeleteButton="True"></asp:CommandField>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBUserConnectionString %>"
SelectCommand="SELECT ... FROM ... "
DeleteCommand="DELETE FROM ... WHERE ...=#...;">
</asp:SqlDataSource>
Since I'm new to VB and ASP I'm using the sqlDatasource to fill the Gridview without VB code behind(please note this), everything is being shown in the aspx code. When I run it with browser and see the source code it only shows html and alot of javascript:__doPostBack's, no connection, no tables, not even asp controls.
However i'd like to know if this is the right way to fill griviews and if it is really safe, I mean is there a way see .aspx code on browser?
It's equally safe as putting it in code behind. However, you really should be putting this sort of thing in code behind so you can separate your logic from your view.
I don't generally put my SQL queries in codebehind as a matter of habit. I prefer to use webservices called by a Data Portal class which call stored procedures.
This is my gridview
<asp:GridView ID="MathGridView" Width="1230px" runat="server" CellPadding="20"
ForeColor="#333333" AutoGenerateColumns="false"
GridLines="None">
<Columns>
<asp:HyperLinkField HeaderText="Name" DataNavigateUrlFields="Name" DataTextField="Name" HeaderStyle-HorizontalAlign="Left"/>
</Columns>
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<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>
There is one hyperlink column present called Name. Now what i want is when i click any content of name(i.e. Any file with extension .pdf or.doc), I want to open that content in either acrobat reader(in case of pdf) or in a new tab in browser.Here the contents i.e.files are not stored in a database. How can i do this??
OK - what you're missing is pointing the hyperlink to correct location; add this to your asp:HyperLink element:
DataNavigateUrlFormatString="~/Books/Math/{0}"