I am building a news page in a web tool using c# and visual studio. The idea is that you upload news in a specific excel format (newsID, Date, Header and text columns) and can view it in the web tool.
I managed to be able to upload the news, store it in a database table and print the news in a Gridview table with three columns: date, header and text. See the below screenshot:
My problem: is that currently I can only print plain text in the Gridview table; but I want to be able to insert hyperlinks in the text column. Is it possible to do this without hard-coding the full HTML for the link into the news message before uploading it?.
This is my default.aspx part of the table:
<div style="width: 100%; height: 400px; overflow-y: scroll">
<asp:GridView CssClass="gridview" ID="News" runat="server" AutoGenerateColumns="false" ShowHeader="False">
<Columns>
<asp:BoundField DataField="Date" ItemStyle-Width="150px" ItemStyle-VerticalAlign="Top"/>
<asp:BoundField DataField="Header" ItemStyle-VerticalAlign="Top"/>
<asp:TemplateField >
<ItemTemplate>
<div class="centered" style="width: 800px; overflow: auto; word-break: break-word; word-wrap: break-word; height: 70px;">
<%# Eval("Text")%>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
And this the c# code behind in default.aspx.cs:
public partial class _Default : Page
{
public void Page_load(object sender, EventArgs e)
{
// Enable News page only for logged in users
if (HttpContext.Current.User.Identity.IsAuthenticated)
{
GetData();
}
}
protected void GetData()
{
News.Columns[0].Visible = true;
string Connection = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
using (SqlConnection con = new SqlConnection(Connection))
{
using (SqlCommand cmd = new SqlCommand("SELECT [Date], [Header], [Text] FROM [ART].[News Indicator] ORDER BY [NewsID] DESC"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
DataSet ds = new DataSet();
sda.Fill(ds);
News.DataSource = ds;
News.DataBind();
}
}
}
}}}
If you set the AutoGenerateColumns property to False, you have a lot of control over the columns and you can control the content.
Then use ItemTemplate with HyperLink:
<asp:GridView AutoGenerateColumns="False">
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink NavigateUrl='www.microsoft.com' Text='Whatever you want to be visible'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
Now the user sees on the screen the value of the Text property, but on click it takes him to page which is the value of NavigateUrl.
Related
I'm trying to create a web page with the aps.net framework and I connect to the SQL server successfully and I want to display the data from the database in the Grid View and there are a search box and dropdown list but there is an error when I try to search
this is the error message:
Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Both DataSource and DataSourceID are defined on 'GridView1'. Remove one definition.
and my code viewpage1.aspx
the GridView
<asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" Format="dd/MM/yyyy" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="InvoiceID" DataSourceID="SqlDataSource3">
<AlternatingRowStyle BackColor="#CCCCCC" />
-and this the sqldatasource
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:connStr %>" SelectCommand="Select * from [Portal].[fn_GetInvoices] (#SearchText) where CompanyCode=#CompanyCode and InvoiceDate between #fromDate and #toDate">
<SelectParameters>
viewpage1.aspx.cs
sqlcomm.CommandText = sqlquery;
sqlcomm.Connection = sqlconn;
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(sqlcomm);
adapter.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
This is a common error message.
I OFTEN use the wizards to build a gridview I use this:
From above, I choose create new data source.
I let the wizard run.
BUT THEN we wind up with some MESSAY sqldatasource in the web page. I do NOT like them, and they NEAR ALWAYS just cause you pain.
So, what I will then do this:
Remove the sqldata source from the web markup - you don't' need that mess anyway.
eg this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:CSharpWebApp.Properties.Settings.TEST4 %>"
SelectCommand="SELECT [ID], [Fighter], [Engine], [Thrust], [Description], [ImagePath]
FROM [Fighters]"></asp:SqlDataSource>
DELETE the above!!!!
Now WHEN you WILL use code to load up the Gridview? Then you ALSO must turn off (remove) the fact that you NOT GOING to use the sql data source anymore.
So in your GV, remove this:
So, all that error message is telling you is you are trying to set the data source in "code" but you ALREADY have a sql datasource setup in the markup.
So, get in the habit of blowing out and removing the sqldata source on the page, and ALSO remove the GV data source setting in the markup.
So, now say I will have this GV, and NOT have ANY data source in the markup in the web page. (and I recommend you do this). So, I still VERY often use the wizards. Even for a dropdown list, a gridview, repeaters, and even listview (my favorite).
So, run wizards - build the markup
Then blow out (remove) the SqlDataSource, and the
DataSourceID="SqlDataSource1" from the control (in this case gv).
So, as noted, you cannot have BOTH a DataSourceID="SqlDataSource1" and THEN try to use code. It is one or the other - and that's what the error message is telling you.
So, now we have this markup:
(and the wizards generated most of this for me!!!)
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" >
<Columns>
<asp:BoundField DataField="Fighter" HeaderText="Fighter" />
<asp:BoundField DataField="Engine" HeaderText="Engine" />
<asp:BoundField DataField="Thrust" HeaderText="Thrust" />
<asp:BoundField DataField="Description" HeaderText="Description" />
<asp:TemplateField HeaderText="View">
<ItemTemplate>
<asp:ImageButton ID="btnImage" runat="server" Height="68px" Width="149px"
OnClientClick ="popimage(this);return false"
ImageUrl = '<%# Eval("ImagePath") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Just nice clean markup - no Sqldatasource junk.
Now we are free to write normal code like a normal human, and we can fill the grid like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid();
}
void LoadGrid()
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters", conn))
{
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
GridView1.DataSource = rstData;
GridView1.DataBind();
}
}
}
And our results are now this:
Ok, so now lets add a search box above the GV to search the GV.
Say you can type in the first few chars of the Fighter jet name, and we want to filter by that:
So, drop in a text box above the GV, + search button.
We now have say this:
<asp:Label ID="Label1" runat="server" Text="Search for Fighter jet" Font-Size="Large"></asp:Label>
<asp:TextBox ID="txtSearch" runat="server" Style="margin-left:15px" Font-Size="Large"></asp:TextBox>
<asp:Button ID="cmdSearch" runat="server" Text="search"
style="margin-left:15px" CssClass="btn"
/>
<br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ID" CssClass="table" >
<Columns>
So, now the page with searching looks like this:
Say we search for Lockheed - but even just typing in Lock would be fine.
thus:
And now our code can say be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadGrid("");
}
void LoadGrid(string MySearch)
{
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.TEST4))
{
using (SqlCommand cmdSQL = new SqlCommand("SELECT * from Fighters ", conn))
{
if (MySearch != "")
{
cmdSQL.CommandText += #" WHERE Fighter LIKE #Fighter + '%'";
cmdSQL.Parameters.Add("Fighter", SqlDbType.NVarChar).Value = MySearch;
}
conn.Open();
DataTable rstData = new DataTable();
rstData.Load(cmdSQL.ExecuteReader());
GridView1.DataSource = rstData;
GridView1.DataBind();
}
}
}
protected void cmdSearch_Click(object sender, EventArgs e)
{
LoadGrid(txtSearch.Text);
}
}
So, I am STRONG suggesting that you DUMP the sqldata source on the web page markup. Such Sqldata soruces on the page can be great for one time load or data display. But the VERY instant you want control, filters, and need to use code?
Quite much the SAME instant it is time to drop and toss out and remove the data source from the web markup. You be glad you did, and as you can see, you now have 100% EASY AND SIMPLE control of the data you shove into the GV, and that includes endless possible use of simple code and buttons to add filters etc. to that data.
I have gridview showing columns BusRoute,BusNo and Action.Where action contains the linkbuttons to display another gridview.I want to display it in jquery dialog.My codes are.
ASPX Code:
First Gridview:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtn" runat="server" OnClientClick="showDialog();">Shipment Status</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</div>
Second Gridview:
<div class="gridview_stop" id="popup">
<asp:GridView ID="Stops" runat="server" AutoGenerateColumns="False" CellPadding="6" Width="190px">
<Columns>
<asp:BoundField HeaderText="Bus Stop" DataField="StopName" HeaderStyle-BackColor="#006B89">
<HeaderStyle BackColor="#006B89" Font-Size="18px" Font-Bold="false"></HeaderStyle>
<ItemStyle BackColor="#E0E0E0" HorizontalAlign="Center" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
Code Behind File:
protected void search_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter();
con.Open();
SqlCommand cmd = new SqlCommand("spGetRoutes", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#BusStop1", Source.Text);
cmd.Parameters.AddWithValue("#BusStop2", Destination.Text);
adapter.SelectCommand = cmd;
adapter.Fill(ds);
adapter.Dispose();
cmd.Dispose();
con.Close();
if (ds.Tables[0].Rows.Count > 0)
{
Route.DataSource = ds.Tables[0];
Route.DataBind();
Stops.DataSource = null;
Stops.DataBind();
Lblmsg.Text = "";
}
else
Lblmsg.Text = "No Direct Bus Between These Stop";
Lblmsg.ForeColor = Color.WhiteSmoke;
Route.Dispose();
Route.DataBind();
Stops.Dispose();
Stops.DataBind();
}
protected void Route_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.backgroundColor='aquamarine';";
e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='white';";
e.Row.ToolTip = "Click last column for selecting this row.";
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string Name = Route.SelectedRow.Cells[0].Text;
SqlDataAdapter adapter1 = new SqlDataAdapter();
DataSet ds1 = new DataSet();
string connetionString = "Data Source=.;Initial Catalog=BusService;uid=sa;Password=Murli#925";
SqlConnection connection = new SqlConnection(connetionString);
connection.Open();
SqlCommand cmd = new SqlCommand("spGetStops", connection);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
int BusNo= Convert.ToInt32(Name);
cmd.Parameters.AddWithValue("#BusNo",BusNo);
adapter1.SelectCommand = cmd;
adapter1.Fill(ds1);
adapter1.Dispose();
cmd.Dispose();
connection.Close();
Stops.DataSource = ds1.Tables[0];
Stops.DataBind();
}
Jquery Function:
<script type="text/javascript">
$("#lnkbtn").live("click",
function showDialog() {
$("#popup").dialog({
show: { effect: "fold", duration: 4000 },
hide: { effect: "fold", duration: 4000 },
});
return false;
});
$(document).click(function (event) {
if (!$(event.target).closest('#popup').length) {
if ($('#popup').is(":visible")) {
$('#popup').dialog('close');
}
}
})
</script>
Thanks & Regards.
Take a look at your rendered html when you run the page. Unless you set ClientIDMode to Static I doubt that your button ID's are $("#lnkbtn") but more like $("#gvWhatever_lnkbtn_0")
Set OnClientClick="showDialog();" or $("#lnkbtn").live("click", function showDialog(){...}) no need to do both.
Using a Server Control button to activate a popup usually doesn't work. Especially if GridView Row Selection is enabled. I can't tell if your first GridView has that feature enabled as I only see a portion of the listing. But what tends to happen is you trigger a postback while trying to display the popup so you never see the popup.
Also your second popup contains a gridview, which requires a databind. so you need to make sure the gridview is populated with the data before activating the popup
Two methods for showing a popup using jquery from within a Gridview:
A portion of a GridView markup with 2 TemplateField's
<asp:TemplateField HeaderText="Info">
<ItemTemplate>
<div>
<div class="rs-icon rs-icon-info tooltip-marker" role="button">
</div>
<div id="ContactInfo" style="display:none;">
<table id="tblContactDetail" class="ContactDetail Note">
<tr>
<td style="width: 80px">Name</td>
<td style="width: 100%">
<asp:Literal ID="Literal1" runat="server"
Text='<%# Eval("expert_name") %>' />
</td>
</tr>
.
.
.
</table>
</div>
</div>
<ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CV">
<ItemTemplate>
<div id="divButtonViewCV" runat="server"
class="rs-icon rs-icon-cv" role="button"
onclick='<%# Eval("expert_cv_id", "ViewPDF({0})") %>' >
</div>
</ItemTemplate>
</asp:TemplateField>
In the above markup I'm setting up for two styles of "popup". The first Template uses jQueryUI ToolTip widget which is activated on mouseover/out. This is nice because it doesn't require a click which means no worries about a postback mucking up the works. The interesting thing here is that I'm using a div as button.
How it works: The div with the .tooptip-marker class is set up to activate a jqueryui tooltip which will display the contents of <div id="ContentInfo"> by grabbing the inner html of the sibling div of the activating button. Note that this div is styled as display:none; so that the content is available for the tooltip but hidden so the main GridView displays as normal.
For brevity, I included only a portion of the content of <div id="ContentInfo">. But It can contain anything really. In my code it's a simple <table> containing contact information fields (using bound <asp:Literal> controls) that are part of the primary GridView data source.
But this could just have easily been an embedded GridView with it's own data source that is bound in the OnRowDatabound event of the primary GridView.
The following jquery sets up the widget. See jQueryUI ToolTip widget for more complete documentation
$( function() {
$( document ).tooltip( {
items: ".tooltip-marker",
content: function() {
return $( this ).next().html();
},
position: {
my: "left top",
at: "right+5 top-5"
}
} )
} );
The second Template field is used to activate a popup. This is where you have to be careful about postbacks. Again I use a <div> styled as an icon and treated as a simple button because I've added onclick.
Clicking this button displays a popup so you can view a persons CV in PDF format. The PDF is stored in our database as a varbinary. Here I use another jquery addon, (colorbox), to display the PDF as a popup over the page.
In the ViewPDF() function below note that we prevent the "bubbling up" of the click event to the GridView by setting cancelBubble = true (for older IE) or a call to stopPropagation() (all other browsers).
In this code, colorbox sets up an iframe and passes the href parameter to its src attribute. I'm making a call to an .ashx page which is an asp Generic Handler which allows us to serve up other type of content without standard web page overhead. This could just as easily been configured to accept a standalone .aspx page where you could maybe place your secondary grid.
// This plain object is used by the call to colorbox(), please
// refer to colorbox documentation for details.
var colorboxDataExpertCV = {
height: "85%",
width: 900,
opacity: .30,
fixed: true,
iframe: true,
returnFocus: false,
href: ''
}
// ========================================================
// In the Template above, the onclick code:
// onclick='<%# Eval("expert_cv_id", "ViewPDF({0})") %>'
//
// Renders to:
// onclick="ViewPDF(12345)"
//
// PdfHandlerExpertsCV.ashx is an GenericHandler that retrieves
// the CV from our database and writes the byte array to the
// colorbox iframe as an "application/pdf" content type which
// triggers native browser pdf management either by internal
// viewer or installed PDF plugin
// ========================================================
function ViewPDF( p ) {
if ( event.stopPropagation )
event.stopPropagation();
else
event.cancelBubble = true;
if ( p && p > 0 ) {
colorboxDataExpertCV.href = "/PdfHandlerExpertsCV.ashx?cvid=" + p;
$.colorbox( colorboxDataExpertCV );
}
return false;
}
Addendum
Let me make it more clear, I need to bind the data in 2nd Gridview and
display this Gridview2 in Jquery dialog .This data will be binded and
displayed in dialog from on the click event of linkbutton inside
Gridview1
As I said above, you cannot bind a GridView (server side event) and then popup a jquery ui dialog (client side event) without introducing much more complexity. You have to make sure all the data you need for the popup is available by the time you're ready to hit the button that opens the dialog.
The simplest way to accomplish this is to embed GridView2 within GridView1 and take care of the binding all at once. It's not pretty or efficient and, depending on the number of items shown in each, can slow down the page. But if you have only a few lines for each it should be acceptable.
Embed GridView2 and DataSource2 in the <ItemTemplate> of a <TemplateField> of GridView1.
Bind GridView2 in the RowDataBound event of Gridview1 using an appropriate row field or datakey field from GridView1 and then calling GridView2.DataBind().
Add a element to the same TemplateField
Refer to the jquery dialog modal form example which shows how to trigger a dialog from a button click. In your case substitute the second GridView for the Form in the example.
IMPORTANT be mindful that gridview row item id's get "mangled" so you should not use id's in the jquery calls but use jquery selectors that are based on element name plus class markers, ie: $("button.marker") for a button defined like this: <button class="marker">.
Addendum: 2015-Jun-24
CSS (<head> tag)
<link rel="stylesheet" type="text/css" href="css/smoothness/jquery-ui.css" />
<script type="text/javascript" src="Scripts/jquery-2.1.4.min.js"> </script>
<script type="text/javascript" src="Scripts/jquery-ui-1.11.4.min.js"> </script>
<style>
.btn_styling {
text-align: center;
width: 32px;
margin: 2px;
cursor: pointer;
border: 1px solid gray;
border-radius: 3px;
/* add a background image to the div
to make the div look like a button */
/* background-image: url('...') */
}
.ui-dialog-titlebar-close {
display: none;
}
</style>
HTML (.aspx)
<form id="form1" runat="server">
<div style="width: 400px;">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="user_id"
DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="user_name" HeaderText="User Name" SortExpression="user_name" />
<asp:TemplateField HeaderText="Info">
<ItemTemplate>
<div id="divButton" runat="server" class="btn_styling dialog-marker" title="This could also have been a <button> element or maybe an <img> element...anything really">X</div>
<div style="display: none;">
<asp:GridView ID="GridView2" runat="server"
AutoGenerateColumns="False"
DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="email" HeaderText="Email" SortExpression="email" />
<asp:BoundField DataField="add_edit_date" HeaderText="Date Added" SortExpression="add_edit_date" DataFormatString='{0:dd-MMMM, yyyy}'/>
<asp:BoundField DataField="add_edit_by" HeaderText="Added By" SortExpression="add_edit_by" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT email, add_edit_date, add_edit_by FROM tbl_users WHERE (user_id = #user_id)">
<SelectParameters>
<asp:Parameter Name="user_id" />
</SelectParameters>
</asp:SqlDataSource>
</div>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BorderColor="Blue" BorderStyle="Solid" BorderWidth="1px" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT top 10 user_id, user_name from tbl_users">
</asp:SqlDataSource>
</div>
<div id="dialogContainer">
</div>
</form>
Code Behind (VB):
Private Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
Dim gv2 As GridView = DirectCast(e.Row.FindControl("GridView2"), GridView)
Dim sds As SqlDataSource = DirectCast(e.Row.FindControl("SqlDataSource2"), SqlDataSource)
sds.SelectParameters("user_id").DefaultValue = GridView1.DataKeys(e.Row.RowIndex).Value
gv2.DataBind()
End If
End Sub
jQuery to implement Dialog:
<script type="text/javascript">
var dialogOptions = {
autoOpen: false,
appendTo: "#dialogContainer",
modal: true,
height: "auto",
width: "auto",
title: "Dialog Title",
closeOnEscape: true,
buttons: {
Cancel: function() {
$( this ).dialog( "close" );
}
}
};
$( function() {
$( ".dialog-marker" ).on( "click", function() {
var d = $( this ).next( "div" ).first().dialog( dialogOptions );
d.dialog( "open" );
} );
} );
</script>
I have a nested repeater on my page like:
Aspx page:
<asp:Repeater ID="parentRepeater" runat="server">
<ItemTemplate>
<br />
<b><%# DataBinder.Eval(Container.DataItem,"question") %></b><br>
<!-- start child repeater -->
<asp:Repeater ID="childRepeater" DataSource='<%# ((DataRowView)Container.DataItem).Row.GetChildRows("relation") %>'
runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "[\"AnsId\"]")%>'><%# DataBinder.Eval(Container.DataItem, "[\"Ans\"]")%></asp:LinkButton>
<br>
</ItemTemplate>
</asp:Repeater>
<!-- end child repeater -->
</ItemTemplate>
</asp:Repeater>
Code behind:
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["star_report_con"].ConnectionString);
SqlDataAdapter cmd1 = new SqlDataAdapter("select questionId, question from questions", cnn);
//Create and fill the DataSet.
DataSet ds = new DataSet();
cmd1.Fill(ds, "questions");
//Create a second DataAdapter for the Titles table.
SqlDataAdapter cmd2 = new SqlDataAdapter("SELECT AnsId, Ans, questionId FROM answers", cnn);
cmd2.Fill(ds, "answers");
//Create the relation bewtween the Authors and Titles tables.
ds.Relations.Add("relation",
ds.Tables["questions"].Columns["questionId"],
ds.Tables["answers"].Columns["questionId"]);
//Bind the Authors table to the parent Repeater control, and call DataBind.
parentRepeater.DataSource = ds.Tables["questions"];
Page.DataBind();
//Close the connection.
cnn.Close();
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "MyUpdate")
{
//e.CommandArgument --> contain the Ansid value
//I want to also find which questions ans is clicked i.e the questionId
}
}
In my child repeater I have a linkbutton on click of which i need to do some computaion for which I need to know which questions answers wa being clicked. i.e in my LinkButton1_Command I want to fetch the AnsId along with QuestionId.
How will I get parent repeaters Id in button click event?
Try this ,
<%# ((RepeaterItem)Container.Parent.Parent).DataItem %>
If this does not work then try
<%# DataBinder.Eval(Container.Parent.Parent, "DataItem.YourProperty")%>
if you're in code-behind in the ItemDataBound method:
((Repeater)e.Item.NamingContainer.NamingContainer).DataItem
I'm relatively new to ASP.NET. My problem is I am trying to create a GridView and bind data to it by using a DataTable. My GridView element shows up in the design mode of VS 2012, but when I run it in the browser(IE), nothing displays. I have bound the data, there is clearly data entered in and I even have the EmptyDataText set to a value, so I am confused as to why NOTHING is displaying on the page from the GridView element. If I set other labels outside of GridView it displays fine, so I do not believe it is a hosting issue. Even when I turn the AutoGenerateColumns value to true, nothing happens. Any help at all would be extremely appreciated.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Tester.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>This is my page.</title>
<style type="text/css">
table {
border: 2px dashed #00FF00;
padding: inherit;
margin: inherit;
width: auto;
height: auto;
top: auto;
right: auto;
bottom: auto;
left: auto;
background-color: #0000FF;
color: #FFFFFF;
font-weight: bold;
}
</style>
</head>
<body>
<form runat="server" id="MyForm">
<asp:GridView AutoGenerateColumns="false" ID="gv" runat="server" Width="1000px" Visible="true" BorderColor="Red" EmptyDataText="WHERE IS MY DATA???">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" Text="testing123">Label from GridView</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="VenLogo" HeaderText="ID" />
<asp:BoundField DataField="VenName" HeaderText="Website" />
<asp:BoundField DataField="VenWeb" HeaderText="URL" HtmlEncode="false" />
</Columns>
</asp:GridView>
</form>
</body>
</html>
Here is my CodeBehind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Text;
namespace Tester
{
public partial class Default : System.Web.UI.Page
{
GridView gv = new GridView();
protected void Page_Load(object sender, ObjectDataSourceStatusEventArgs e)
{
if (!Page.IsPostBack)
{
gv.DataSource = Datatable();
gv.DataBind();
gv.Visible = true;
}
}
private DataTable Datatable()
{
DataTable datatable = new DataTable();
datatable.Columns.Add("VenLogo", typeof(string));
datatable.Columns.Add("VenName", typeof(string));
datatable.Columns.Add("VenWeb", typeof(string));
AddNewRow("Logo URL", "google", "http://google.com", datatable);
AddNewRow("Logo URL", "facebook", "http://facebook.com", datatable);
return datatable;
}
private void AddNewRow(string id, string website, string url, DataTable table)
{
DataRow row = table.NewRow();
row["VenLogo"] = id;
row["VenName"] = website;
//get url from GetURL method
string link = GetURL(website, url);
row["VenWeb"] = HttpUtility.HtmlDecode(link);
table.Rows.Add(row);
}
private string GetURL(string website, string url)
{
return "" + website + "";
}
}
}
Image of Split View in VS.
I am not sure, do you want to add the gridview dynamically to the page or use the one from your markup? If the first, you need to add the statement
MyForm.Controls.Add(gv);
to Page_Load. If the latter, you don´t need
GridView gv = new GridView();
but can just reference gv from markup by it´s ID.
By the way, you also have to change the parameter type of the Page_Load:
protected void Page_Load(object sender, EventArgs e)
Your local instance of gv is screwing things up. Check other parts of your partial class for a definition of gv. I'm guessing that you Page_Load code is binding to a local, private instance, instead of the protected instance that the page is using for the control. You'll want something like the following:
protected global::System.Web.UI.WebControls.GridView gv;
Your CS file code would be like this.
protected void Page_Load(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.DataSource = Datatable();
gv.DataBind();
gv.Visible = true;
MyForm.Controls.Add(gv);
}
private DataTable Datatable()
{
DataTable datatable = new DataTable();
datatable.Columns.Add("VenLogo", typeof(string));
datatable.Columns.Add("VenName", typeof(string));
datatable.Columns.Add("VenWeb", typeof(string));
AddNewRow("Logo URL", "google", "http://google.com", datatable);
AddNewRow("Logo URL", "facebook", "http://facebook.com", datatable);
return datatable;
}
private void AddNewRow(string id, string website, string url, DataTable table)
{
table.Rows.Add(id, website, url);
}
private string GetURL(string website, string url)
{
return "" + website + "";
}
I have a button on my ASP.NET page, which fetches some data from my database and displays it on a gridview.
This process takes a while, so I thought I'll add an updateprogress AJAX control. Now when I click the button, the updateprogress image shows up and data is being fetched from my database successfully (I checked this from some logs that I have in my DB). But there are 2 issues:
(1) The updateprogress image shows only for about 2 minutes. But my buttonclick event takes about 5 minutes to complete. Basically the updateprogress stops showing up even before my task is complete, which defeats its purpose.
(2) GridView doesn't show up. It shows up correctly if I don't use scriptmanager/AJAX.
Any ideas?
Some relevant code snippets:
<div style="position: absolute; top: 300px; left: 19px; width: 568px; height: 48px;">
<table>
<tr>
<td>
<asp:Button ID="btnGenerateReport" runat="server" Height="37px" Text="Generate Report"
Width="132px" onclick="btnGenerateReport_Click" />
</td>
<td class="style5">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" id="Panel">
<ContentTemplate>
<asp:UpdateProgress ID="PageUpdateProgress" runat="server">
<ProgressTemplate>
<img runat="server" src="updateprogress.gif"
style="position: static; width: 32px;"> </img></ProgressTemplate>
</asp:UpdateProgress>
<div style="position: absolute; top: 423px; left: 9px; width: 795px; height: 984px;">
<asp:GridView ID="Report" runat="server"
AllowSorting="True" AutoGenerateColumns="False"
Height="622px" BorderStyle="Solid"
Width="779px" PageSize="100" HorizontalAlign="Center">
<Columns>
<asp:BoundField HeaderText="AccountId" DataField="AccountId">
<ControlStyle BorderStyle="Solid" Width="20px" />
</asp:BoundField>
<asp:BoundField HeaderText="User Name" ReadOnly="True" DataField="UserName">
<ControlStyle Width="50px" />
</asp:BoundField>
<asp:BoundField HeaderText="C2" ReadOnly="True"
DataField="C2">
<ControlStyle BorderStyle="Solid" Width="20px" />
</asp:BoundField>
<asp:BoundField HeaderText="C1" ReadOnly="True"
DataField="C1">
<ControlStyle BorderStyle="Solid" Width="20px" />
</asp:BoundField>
</Columns>
</asp:GridView>
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnGenerateReport" EventName="click" />
</Triggers>
</asp:UpdatePanel>
</td>
<td>
<asp:Button ID="btnExportToExcel" runat="server" Text="Export To Excel"
Height="37px" Width="132px" onclick="btnExportToExcel_Click" />
</td>
</tr>
</table>
</div>
Codefile part:
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
FetchAccounts();
long startId = FetchAuditData();
AggregateAuditData(startId);
dsAnalysisReport = GenerateDataSet();
if (dsAnalysisReport == null || dsAnalysisReport.Tables.Count == 0)
lblErrorText.Text = "No Data Found for the input information";
else
BindData(dsAnalysisReport);
}
public void FetchAccounts()
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
cmdstring = #"[spo_FetchAccounts]";
cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
cmd.CommandTimeout = 100000;
cmd.Parameters.Add("#MaxActivationDate", SqlDbType.DateTime);
cmd.Parameters["#MaxActivationDate"].Value =
DateTime.Parse(txtStartDate.Text) - new TimeSpan(1, 0, 0, 0);
cmd.ExecuteNonQuery();
sqlConnection.Close();
}
}
public long FetchAuditData()
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
cmdstring = #"[spo_GetComparisonData]";
cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
cmd.CommandTimeout = 100000;
cmd.Parameters.Add("#StartDate", SqlDbType.DateTime);
cmd.Parameters["#StartDate"].Value = txtStartDate.Text;
cmd.Parameters.Add("#EndDate", SqlDbType.DateTime);
cmd.Parameters["#EndDate"].Value = txtEndDate.Text;
SqlParameter returnValue = new SqlParameter("#Return_Value", SqlDbType.BigInt);
returnValue.Direction = ParameterDirection.ReturnValue;
cmd.Parameters.Add(returnValue);
cmd.ExecuteNonQuery();
long startId = long.Parse(cmd.Parameters["#Return_Value"].Value.ToString());
sqlConnection.Close();
return startId;
}
}
private void AggregateAuditData(long startId)
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
cmdstring = #"[spo_ComparisonTable]";
cmd = new SqlCommand(cmdstring, sqlConnection) { CommandType = CommandType.StoredProcedure };
cmd.CommandTimeout = 100000;
cmd.Parameters.Add("#StartId", SqlDbType.Int);
cmd.Parameters["#StartId"].Value = startId;
cmd.ExecuteNonQuery();
sqlConnection.Close();
}
}
public DataSet GenerateDataSet()
{
using (var sqlConnection = new SqlConnection(ConnectionString))
{
sqlConnection.Open();
cmdstring = #"SELECT * FROM XAccounts";
cmd = new SqlCommand(cmdstring, sqlConnection);
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(ds);
sqlConnection.Close();
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
return ds;
return null;
}
}
private void BindData(DataSet ds)
{
BindReportGrid(Report, ds.Tables[0]);
}
private void BindReportGrid(GridView gridToBind, DataTable dataTableToBind)
{
gridToBind.DataSource = dataTableToBind;
gridToBind.DataBind();
}
As per issue (1) most likely it is ajax timing out. Default timeout is 90 seconds. To increase that use ScriptManager's AsyncPostBackTimeout property:
<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeout="400">
</asp:ScriptManager>
If ajax call is timing out, controls on the page might not work correctly so increasing timeout might solve problem (2) as well.
I have had very same problems with ASP.NET UpdateProgress. I fixed it by handling script manager events directly:
<script language="javascript" type="text/javascript">
//adding event handlers for ajax initialize request and end request
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(ShowHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(HideHandler);
function ShowHandler(sender, args) {
//show div with animation
pcProcessing_ClientInstance.Show();
}
function HideHandler(sender, args) {
//hide div with animation
pcProcessing_ClientInstance.Hide();
}
</script>
Maybe you want this: http://www.codeproject.com/kb/Ajax/ModalUpdateProgress.aspx
It works well for me, even with lengthy operations.
Seems like your grid is outside the Update panel, if you are using a update panel and you button is inside the update panel and grid is outside the update panel in that case everything will happen at server side but you will not find any changes in the UI.
Try to place your grid inside the update panel.
Move the UpdateProgress outside of the UpdatePanel.