How to create a dynamic table from java script? - asp.net

How to create a dynamic table from java script?I am fetching a record from database and storing into variable and adding that value to table cell.
Here I put some text not actual value.
In cs file's button click event I doing like this
StringBuilder sb = new StringBuilder();
sb.Append(#"<script language='javascript'>");
sb.Append("<table><tbody><tr><td>Information</td></tr></tbody></table>");
sb.Append(#"</script>");
if (!Page.ClientScript.IsClientScriptBlockRegistered(Page.GetType(), "HeyPopup"))
ClientScript.RegisterClientScriptBlock(Page.GetType(), "HeyPopup", sb.ToString());
But I am unable to see Information.

you must add document.Write in your script
so replace with
sb.Append("document.write('<table><tbody><tr><td>Information</td></tr></tbody></table>');");

Related

Dynamic hyperlink from code behind asp.net

I want dynamic hyperlink on each field in a table column from code behind in asp.net, I implement it thus:
table.Append("<td><asp:HyperLink ID='HyperLink1' NavigateUrl='#' runat='server'>" + (string)strNAME + "</asp:HyperLink></td>");
on the field but when I run it there is no link to click. it is not effective. what is the correct way of implementing it?
You need to approach this in a different way. Server side controls cannot be added as string literals, they should be objects. So what you can do is either add it as a server side control:
HyperLink hl = new HyperLink();
hl.ID = "HyperLink1";
hl.NavigateUrl = "#";
hl.Text = (string)strNAME;
TableCell tc = new TableCell();
tc.Controls.Add(hl);
table.Controls.Add(tc);
Or add it as a client side link:
table.Append("<td><a href='#'>" + (string)strNAME + "</a></td>");
Side note: adding table cell to "table" kind of does not make sense because there supposed to be a row, not a table, but I just left your code as is, adjust as needed.
Creating hyperlink from code-behind
HyperLink hlnk = new HyperLink();
hlnk.InnerText = (string)strNAME;
hlnk.ID = "HyperLink1";
hlnk.NavigateUrl = "/test.aspx";
table.Controls.Add(hlnk);
hope it helps

Creating a report from label values in asp.net

I have a website that I wrote in ASP.net and I have a page called Statistics.aspx
that I displayed few useful numbers that admin can view in labels .
I want to make a button " Download Report " that can put these numbers into a word/pdf document .
Can anyone help ?
You need to give a bit more detail.Well I believe this is useful
string mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
StringBuilder sb = new StringBuilder();
sb.AppendLine(Label1.text); // and so on
using (StreamWriter outfile = new StreamWriter(mydocpath + #"\AllTxtFiles.txt"))
{
outfile.Write(sb.ToString());
}

ASP GridView All Rows In Edit Mode

In my ASP.NET page, I'm using a GridView to view data (Items & their prices). Currently users can edit the data (prices) in the grid row by row. (Click --> "Edit" link, change the values then "Update"). This is ROW by ROW. Is it possible to open all rows in Edit mode & use a single button (eg. Submit) to update all data once?
If you don't need to read only mode, in that case you can put input boxes ( textbox, dropdownlist, etc.) in ItemTEmplate section and bind them with existing data.
Next, put a submit button at above/below of the GridView and handle button Click event and loop through the GridView item data and save all database.
I'll post code block if you need. Thanks for your time.
And you will have better control on that you doing by using listview instead of gridview.
My best practise is using listview and custom web user control for this kind of problems.
If you fill your listview with your user control, you will easy to manage your saving method, just have to iterate on the listview items, find control and call your Save() method for each item.
I know that this question has already been answered but here is the code for loop through the GridView getting data and store it in the Data Base:
Using libraries:
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Data.Odbc;
Code Behind:
// this is a variable that have the Query or SQL Commands.
string DataBaseQuery = "UPDATE [table] SET [variable2] = #variable2, [variable3] = #variable3) WHERE [variable1] = #variable1";
//Click Event from a LinkButton.
protected void LinkButton1_Click(object sender, EventArgs e)
{
//"ConnectionString" its the string connection for your DataBase (often get from the WebConfig File or a DataSource element.
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
//this is for open the database using the string connection.
connection.Open();
//this is the algorithm for going through the entire GridView.
for (int i = 0; i < GridView1.Rows.Count; i++)
{
//"DataBaseQuery" it's a string variable that have the Query or SQL Commands.
SqlCommand cmd = new SqlCommand(DataBaseQuery, conexion);
//this case it's for obtain the text variable of the first column of the gridview (in my case it was the ID of the register).
cmd.Parameters.AddWithValue("#variable1", ((Label)GridView1.Rows[i].Cells[0].FindControl("Label1")).Text.ToString());
//this case it's for obtain the selected value of a DropDownList that were in the 14 th column)
cmd.Parameters.AddWithValue("#variable2", ((DropDownList)GridView1.Rows[i].Cells[15].FindControl("DropDownlist2")).SelectedValue.ToString());
//this command it's for obtain the text of a textbox that is in the 15 th column of the gridview.
cmd.Parameters.AddWithValue("#variable3", ((TextBox)GridView1.Rows[i].Cells[16].FindControl("TextBox17")).Text.ToString());
cmd.ExecuteNonQuery();
}
//after going through all the gridview you have to close the connection to the DataBase.
connection.Close();
}
}
Of course you have to adjust the code to your particular case but it's very easy. In this code you have the example to obtain values for other object like labes, textbox and dropdownlist in the gridview.
I suffered a lot to make run this code (I'm not good programming) but I'm happy to help.
NOTE: For count the columns of the gridview you have to start at zero.
NOTE2: Sorry for my bad English by the way... It's not my nature language.

send rendered GridView in an email

I have a GridView whose HTML I need to put into an email.
How can this be done?
use an HTMLtext writer to render the gridview control.You will get an HTML output as string.use this as your message body.
public string RenderControl(Control ctrl)
{
StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
return sb.ToString();
}
Use a stringbuilder to create a HTML table to resample your gridview like so:
var output = new StringBuilder("<table cellpadding='5' style='border: solid 1px black;'>");
foreach (GridViewRow row in MyGridView.Rows)
{
output.Append("<tr>");
output.Append("<td>" + Title + "</td>");
output.Append("<td>" + Price + "</td>");
output.Append("<td>" + Quantity + "</td>");
output.Append("</tr>");
}
output.Append("</table>");
Ideally you should not be concerned about the GridView control. Take the data that is being displayed in the control, format it in what ever manner you want and pass it as your message body.
I would treat the logic to display Grid in my application and generating grid for message body as two separate functionality. What will be common in between the two, is the source of data. For formatting of data(generating the Grid), you can use the HTMLTextWriter class(as mentioned by others) and format the data in what ever way you want.
Two thing you can do for this
1) take screen shot of the current page by using any utility and send in mail
2) you can export grid as excel and than sent it in mail.
Export grid to Excel
http://geekswithblogs.net/azamsharp/archive/2005/12/21/63843.aspx

Adding hyperlinks dynamically by code

I have a web form but I have to do this by code since I dont know the number of hyperlinks I need from the beginning.
How can I add some hyperlinks with Image in a label, the number of hyperlink depends on the number of rows of a query, and each row give me the link information to navigate.
Thanks in advance.
As you loop through your data you can manually add a link to the row something like this:
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
row.Cells.Add(New HtmlTableCell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
That of course adds the link as the first cell in an html table. Not sure how you plan to display your data.
In response to the comment below. You can instead insert the new cell something like this
For i As Integer = 0 To 10
Dim row As New HtmlTableRow
Dim cell As New HtmlTableCell
row.Cells.Insert(1, cell)
Dim Link As New HyperLink
Link.Text = "WhateverText"
Link.NavigateUrl = "page.aspx"
Link.ImageUrl = "~/Theme/Images/SomeImage.gif"
Link.ToolTip = "ToolTipText"
row.Cells(0).Controls.Add(Link)
Next
You could also simply add the control to the existing cell that the label is in instead of making a new cell. You can do that by the index value of your existing cell (starting at 0 for each cell that is in the row)
This question is similar to what you want to do:
Auto increment asp control ID
Two options either use a repeater or dynamically add the controls to a panel or some other container control.

Resources