ASP.net GridView doesn't show - asp.net

I have GridView on my page:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server"
onselectedindexchanged="GridView1_SelectedIndexChanged">
</asp:GridView>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>
And i use button to set grid view datasource:
DataAdapter adapter=new DataAdapter(SqlCommand,SqlConn);
DataTable tbl=new Datatable();
adapter.Fill(tbl);
GridView1.DataSource=tbl;
From debug mode i can see that datatable is filled property and does contain data. But i can see nothing in the screen. What's the problem
P.S. Found the simillar question except that in that question no data source was set

You are missing to call databind method here.Use following code :
DataAdapter adapter=new DataAdapter(SqlCommand,SqlConn);
DataTable tbl=new Datatable();
adapter.Fill(tbl);
GridView1.DataSource=tbl;
GridView1.DataBind();
Let me know it is working for you or not ?

Related

Dynamically Loaded Existing Panel cause issue of PostBack

At first See the Code below.
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="test.aspx.vb" Inherits="BLL.test" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="pnlCustom" runat="server"></asp:Panel>
<asp:Panel ID="pnlDiv1" runat="server">
<asp:DropDownList ID="ddl_Status" runat="server"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv2" runat="server">
<asp:DropDownList ID="ddlImg1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg1_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
<asp:Panel ID="pnlDiv3" runat="server">
<asp:DropDownList ID="ddlImg2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlImg2_SelectedIndexChanged"></asp:DropDownList>
</asp:Panel>
</div>
</form>
</body>
</html>
I want to hide and show the panel in different order as per the requirement. i don't want to change the content of the panel. pnlDiv1,pnlDiv2,pnlDiv3 will bind in pnlCustom.
Now, In Page_Load Event i am setting all panel style to "display:none" except "pnlCustom" and dynamically adding pnlDiv1/pnlDiv2/pnlDiv3 in "pnlCustom" and set its style to "display:inline".
In my application problem is If i change the value of "ddlImg1" the page gets postback and all the value reset. this is same for the "ddlImg2".
Note:Values are bind in dropdown in Page_Load if its not postback.. so, anyone can explain what's an issue?
This could be a possible issue with the ViewState.
The following link might be helpful to you:
ASP.NET DropDownList not retaining selected item on postback

Populate Literal elements with the results of a SqlDataSource query

I have what is possibly a very simple question yet the answer is escaping me completely. I'm new to ASP.NET (vb) but I have a solid Classic ASP/VB background.
I have a relatively simple database which contains just four columns (Language, SiteFooterPrivacyPolicy, SiteFooterTermsAndConditions, SiteFooterCopyright). I wish to perform a query which will return just 1 row.
Here is the main page so far (along with the SQL SELECT statement):
<%# Page Language="VB" AutoEventWireup="false" CodeFile="dbtest1.aspx.vb" Inherits="dbtest1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="MyLanguageDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:customer_support_devConnectionString %>"
ProviderName="<%$ ConnectionStrings:customer_support_devConnectionString.ProviderName %>"
SelectCommand="SELECT [SiteFooterPrivacyPolicy], [SiteFooterTermsAndConditions], [SiteFooterCopyright] FROM [language_file_2] WHERE ([Language] = 'French')">
</asp:SqlDataSource>
<br />
<br /><b>Literal1PrivPolicy:</b> <asp:Literal ID="Literal1PrivPolicy" runat="server"></asp:Literal>
<br /><b>Literal2TermsConds:</b> <asp:Literal ID="Literal2TermsConds" runat="server"></asp:Literal>
<br /><b>Literal3Copyright:</b> <asp:Literal ID="Literal3Copyright" runat="server"></asp:Literal>
<br />
</div>
</form>
</body>
</html>
And so far I have this code behind (which does not work) but hopefully it'll explain far better than I can, what I am trying to achieve:
Partial Class dbtest1
Inherits System.Web.UI.Page
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
Dim dv As New Data.DataView
dv = MyLanguageDataSource.Select(DataSourceSelectArguments.Empty)
Literal1PrivPolicy.Text = dv.Table.Rows("SiteFooterPrivacyPolicy").ToString()
Literal2TermsConds.Text = dv.Table.Rows("SiteFooterTermsAndConditions").ToString()
Literal3Copyright.Text = dv.Table.Rows("SiteFooterCopyright").ToString()
End Sub
End Class
Hopefully from that you can see what I'm trying to do. I basically need my Literal elements populated with the correct column returned from my database query.
I think you're trying to access the rows by name, when you should be accessing the first row's columns by name, e.g:
Literal1PrivPolicy.Text = dv.Table.Rows(0)("SiteFooterPrivacyPolicy").ToString()

Viewstate and TabPanel

I have been working on a asp.net project last ~6 months where that was one .aspx that was being loaded with different controls. This page and those controls had their own UpdatePanels, etc etc. In other words I had to deal with a bag of viewstate issues. It seems like whenever I think I get viewstate and its details completely I get something like what I am about to describe below. This might have to do with control state, which is from what I understand "necessary" viewstate you cannot turn off. Or this might have to do something with AJAX.
Anyway take a look at this example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" EnableViewState="false" Inherits="ControlDisabledViewStateTesting._Default" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajx" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script language="C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (treeView.SelectedNode != null)
ContentPanel.Controls.Add(ContentPanel.TemplateControl.LoadControl("MyUserControl.ascx"));
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<ajx:ToolkitScriptManager runat="server" />
<asp:TreeView ID="treeView" runat="server">
<Nodes>
<asp:TreeNode Text="First Node" Value="111"/>
<asp:TreeNode Text="Second Node" Value="222"/>
</Nodes>
</asp:TreeView>
<Asp:Panel ID="ContentPanel" runat="server" />
</div>
</form>
</body>
</html>
And here is the user control mark up:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="MyUserControl.ascx.cs" Inherits="ControlDisabledViewStateTesting.MyUserControl" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajx" %>
<ajx:TabContainer ID="tabContainer" ActiveTabIndex="0" runat="server">
<ajx:TabPanel ID="tab" HeaderText="Tab1" runat="server"/>
<ajx:TabPanel ID="tab2" HeaderText="Tab2" runat="server" />
</ajx:TabContainer>
As you have noticed, Viewstate is turned off on the page level, so none of the controls should be using viewstate.
Click on "First Node".
User control gets loaded with two tabs.
Select the second tab.
Click on "Second Node"
User controls get loaded again, with the "SECOND TAB" selected already.
Is this an issue with viewstate/controlstate or does it have something to do with AJAX part of the TabPanel?
I really appreciate if someone can shed some lights as to what is happening here and how I can turn off this functionality.
Thanks,
Mike
The value for the active tab isn't being stored in view state. It appears to have to do with the ASP.NET AJAX framework and the value is being loaded from the post data.
To change the behavior you could derive from the TabContainer and override the LoadClientState method so that the ActiveTabIndex isn't changed.
You can view the source code for the TabContainer on codeplex:
http://ajaxcontroltoolkit.codeplex.com/SourceControl/changeset/view/1014bf767f65#Server%2fAjaxControlToolkit%2fTabs%2fTabContainer.cs

asp:Label with nested control disappears on setting CssClass

Is it improper to nest another control within asp:Label?
When I have another control (e.g., a button) inside my asp:Label, and I try to change CssClass on the label during an event handler, the entire label seems to disappear. I'll be grateful if someone can help me find what I might be doing wrong.
Bad1.aspx:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Bad1.aspx.cs" Inherits="Bad1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Badness?</title>
</head>
<body>
<form id="form1" runat="server">
<asp:CheckBox ID="CheckBox1" runat="server" Text="check and uncheck me" AutoPostBack="True" OnCheckedChanged="CheckBox1_CheckedChanged" />
<br />
<br />
<asp:Label ID="Label1" runat="server" > See me now? <asp:Button runat="server" /> </asp:Label>
</form>
</body>
</html>
Bad1.aspx.cs:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Bad1 : System.Web.UI.Page
{
protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
{
var lab = Page.FindControl("Label1") as Label;
lab.CssClass = "foo";
}
}
Symptom: Page displays correctly at first, and after first click on "check and uncheck me". But when I click on the checkbox a second time, the label ("See me now") disappears, never to come back. If I remove the button from within the label, there's no problem. So, is the nesting what's incorrect?
I would look into using a Panel instead of a Label.
<asp:Panel ID="Panel1" runat="server" CssClass="someStyle">
<asp:Button ID="Button1" runat="server" Text="Hi"></asp:Button>
</asp:Panel>
If you're looking to style the button, then you should apply your CssClass to the button directly. Use a Panel if you're trying to apply a style to that region/block.

How to assign a method's output to a textbox value without code behind

How do I assign a method's output to a textbox value without code behind?
<%# Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Public TextFromString As String = "test text test text"
Public TextFromMethod As String = RepeatChar("S", 50) 'SubSonic.Sugar.Web.GenerateLoremIpsum(400, "w")
Public Function RepeatChar(ByVal Input As String, ByVal Count As Integer)
Return New String(Input, Count)
End Function
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<%=TextFromString%>
<br />
<asp:TextBox ID="TextBox1" runat="server" Text="<%# TextFromString %>"></asp:TextBox>
<br />
<%=TextFromMethod%>
<br />
<asp:TextBox ID="TextBox2" runat="server" Text="<%# TextFromMethod %>"></asp:TextBox>
</div>
</form>
</body>
</html>
it was mostly so the designer guys could use it in the aspx page. Seems like a simple thing to push a variable value into a textbox to me.
It's also confusing to me why
<asp:Label runat="server" ID="label1"><%=TextFromString%></asp:Label>
and
<asp:TextBox ID="TextBox3" runat="server">Hello</asp:TextBox>
works but
<asp:TextBox ID="TextBox4" runat="server"><%=TextFromString%></asp:TextBox>
causes a compilation error.
There's a couple of different expression types in .ASPX files. There's:
<%= TextFromMethod %>
which simply reserves a literal control, and outputs the text at render time.
and then there's:
<%# TextFromMethod %>
which is a databinding expression, evaluated when the control is DataBound(). There's also expression builders, like:
<%$ ConnectionStrings:Database %>
but that's not really important here....
So, the <%= %> method won't work because it would try to insert a Literal into the .Text property...obviously, not what you want.
The <%# %> method doesn't work because the TextBox isn't DataBound, nor are any of it's parents. If your TextBox was in a Repeater or GridView, then this method would work.
So - what to do? Just call TextBox.DataBind() at some point. Or, if you have more than 1 control, just call Page.DataBind() in your Page_Load.
Private Function Page_Load(sender as Object, e as EventArgs)
If Not IsPostback Then
Me.DataBind()
End If
End Function
Have you tried using an HTML control instead of the server control? Does it also cause a compilation error?
<input type="text" id="TextBox4" runat="server" value="<%=TextFromString%>" />

Resources