aligning left and right in same line dynamically asp.net - css

I am Displaying Picture dynamically in gallery ,below picture i displayed product name,product weight , product color, product quality,origin and quantity.It is currently displayed in 6 lines,each item per line...
I want to display two items in each line and 6 item in 3 lines.First item should align left , second item on right on first line and so on.how can i do it? this is my code.
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class jewellery : System.Web.UI.Page
{
public void select_data()
{
//String con_str = #"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\asim\bootstrap1 (1)\App_Data\Database.mdf;Integrated Security=True;User Instance=True";
String con_str = #"Data Source=KHAN-PC\SQLEXPRESS;Initial Catalog=D:\ASIM\BOOTSTRAP1 (1)\APP_DATA\DATABASE.MDF;Integrated Security=True";
//string con_str = #"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\project\App_Data\Database.mdf;Integrated Security=True";
SqlConnection con = new SqlConnection(con_str);
con.Open();
string query = "select * from jewellery1";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataReader reader = cmd.ExecuteReader();
int n = 1;
String id = "Image";
while (reader.Read())
{
System.Web.UI.HtmlControls.HtmlGenericControl section = new System.Web.UI.HtmlControls.HtmlGenericControl("section");
section.ID = "portfolio";
section.Attributes["class"] = "bg-light-gray";
System.Web.UI.HtmlControls.HtmlGenericControl row = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
row.Attributes["class"] = "row";
Byte[] bytes = (Byte[])reader["item_image"];
String item_name = (String)reader["item_name"];
String color = (String)reader["item_color"];
String weight = (String)reader["item_weight"];
String quality = (String)reader["item_quality"];
String clarity = (String)reader["item_clarity"];
String origin = (String)reader["item_origin"];
System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
createDiv.ID = "createDiv" + n + 11;
System.Web.UI.HtmlControls.HtmlGenericControl createDiv1 =
new System.Web.UI.HtmlControls.HtmlGenericControl("div");
System.Web.UI.HtmlControls.HtmlGenericControl hover = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hover.Attributes["class"] = "portfolio-hover";
System.Web.UI.HtmlControls.HtmlGenericControl hover_content = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hover_content.Attributes["class"] = "portfolio-hover-content";
System.Web.UI.HtmlControls.HtmlGenericControl i = new System.Web.UI.HtmlControls.HtmlGenericControl("i");
i.Attributes["class"] = "fa fa-2x";
createDiv1.ID = "createDiv" + n + 12;
createDiv1.Attributes["class"] = "col-md-4 col-sm-6 portfolio-item zoomIn animated";
createDiv1.Attributes["data-wow-duration"] = "1500ms";
createDiv1.Attributes["data-wow-delay"] = "100ms";
createDiv.Attributes["class"] = "portfolio-caption";
LinkButton a = new LinkButton();
a.Attributes["class"] = "portfolio-link";
a.Attributes["data-toggle"] = "modal";
a.Click += new EventHandler(test);
a.CommandArgument = item_name;
/* System.Web.UI.HtmlControls.HtmlGenericControl a = new System.Web.UI.HtmlControls.HtmlGenericControl("a");
a.Attributes["class"] = "portfolio-link";
a.Attributes["data-toggle"] = "modal";*/
System.Web.UI.HtmlControls.HtmlGenericControl h1 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h2 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h3 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h4 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h5 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h6 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
h1.Attributes["align"] = "LEFT";
h1.Attributes["style"] = "color:black";
h1.InnerHtml = "Name &nbsp &nbsp :&nbsp &nbsp " + item_name;
h2.Attributes["align"] = "RIGHT";
h2.Attributes["style"] = "color:black";
h2.InnerHtml = "color &nbsp &nbsp &nbsp &nbsp :&nbsp &nbsp " + color;
h3.Attributes["align"] = "LEFT";
h3.Attributes["style"] = "color:black";
h3.InnerHtml = "Weight &nbsp &nbsp: &nbsp &nbsp " + weight;
h4.Attributes["align"] = "LEFT";
h4.Attributes["style"] = "color:black";
h4.InnerHtml = "Quality &nbsp&nbsp: &nbsp " + quality;
h5.Attributes["align"] = "LEFT";
h5.Attributes["style"] = "color:black";
h5.InnerHtml = "Clarity &nbsp&nbsp: &nbsp " + clarity;
h6.Attributes["align"] = "LEFT";
h6.Attributes["style"] = "color:black";
h6.InnerHtml = "Origin &nbsp&nbsp: &nbsp " + origin;
System.Web.UI.HtmlControls.HtmlGenericControl hyp = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hyp.Attributes["class"] = "portfolio-link";
System.Web.UI.WebControls.Image tb = new System.Web.UI.WebControls.Image() { ID = "Image" + n + 10 };
tb.Height = 250;
tb.Width = 500;
tb.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes);
hover_content.Controls.Add(i);
a.Controls.Add(hover);
a.Controls.Add(hover_content);
a.Controls.Add(tb);
createDiv.Controls.Add(h1);
createDiv.Controls.Add(h2);
createDiv.Controls.Add(h3);
createDiv.Controls.Add(h4);
createDiv.Controls.Add(h5);
createDiv.Controls.Add(h6);
createDiv1.Controls.Add(a);
createDiv1.Controls.Add(createDiv);
// row.Controls.Add(createDiv1);
//section.Controls.Add(row);
divid1.Controls.Add(createDiv1);
Console.WriteLine("{0}\t{1}", reader.GetInt32(0),
reader.GetString(1));
n++;
}
}
protected void test(object sender, EventArgs e)
{
String wagerId = ((LinkButton)sender).CommandArgument;
Response.Redirect("awesome.aspx?eid=" + wagerId);
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
select_data();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
}
}
Updated Code
Bro your code didn't work. I tried this way,its working somehow fine . It displays H1 on left ,H2 on right.thats fine but it also displays h3 in center,as it should be on next line.how can I resolve it.
h1.Attributes["class"] = "links left";
h1.Attributes["style"] = "color:black";
h1.InnerHtml = "Name &nbsp &nbsp :&nbsp &nbsp " + item_name;
h2.Attributes["class"] = "links right";
h2.Attributes["style"] = "color:black";
h2.InnerHtml = "color &nbsp &nbsp &nbsp &nbsp :&nbsp &nbsp " + color;
h3.Attributes["class"] = "links right";
h3.Attributes["style"] = "color:black";
h3.InnerHtml = "Weight &nbsp &nbsp: &nbsp &nbsp " + weight;
h4.Attributes["align"] = "LEFT";
h4.Attributes["style"] = "color:black";
h4.InnerHtml = "Quality &nbsp&nbsp: &nbsp " + quality;
<style>
.mainlinks
{
text-align:right;
margin-top:3px;
margin-right:10px;
margin-bottom:0px;
padding:0px;
}
a.links:link {
color: #FF0000; text-decoration: none;
text-align:left;
margin-left:8px;
margin-top:300px;
margin-bottom:0px;
padding:2px;
font-family:Arial, Helvetica, sans-serif;
font-size: 14px;
}
.right { float: right }
.left { float: left }
</style>

Use the following code. I've removed Attributes["align"] = "LEFT" part and added float: left;width: 50%;. Adjust the width percentage according to your needs.
h1.Attributes["style"] = "color:black;float: left;width: 50%; ";
h1.InnerHtml = "Name &nbsp &nbsp :&nbsp &nbsp " + item_name;
h2.Attributes["style"] = "color:black;float: left;width: 50%; ";
h2.InnerHtml = "color &nbsp &nbsp &nbsp &nbsp :&nbsp &nbsp " + color;
h3.Attributes["style"] = "color:black;float: left;width: 50%; ";
h3.InnerHtml = "Weight &nbsp &nbsp: &nbsp &nbsp " + weight;
h4.Attributes["style"] = "color:black;float: left;width: 50%; ";
h4.InnerHtml = "Quality &nbsp&nbsp: &nbsp " + quality;
h5.Attributes["style"] = "color:black;float: left;width: 50%; ";
h5.InnerHtml = "Clarity &nbsp&nbsp: &nbsp " + clarity;
h6.Attributes["style"] = "color:black;float: left;width: 50%; ";
h6.InnerHtml = "Origin &nbsp&nbsp: &nbsp " + origin;
UPDATE (Commented out the variables for adjusting in my VS)
var n = 1;
while(n < 5) {
System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
createDiv.ID = "createDiv" + n + 11;
System.Web.UI.HtmlControls.HtmlGenericControl createDiv1 =
new System.Web.UI.HtmlControls.HtmlGenericControl("div");
System.Web.UI.HtmlControls.HtmlGenericControl hover = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hover.Attributes["class"] = "portfolio-hover";
System.Web.UI.HtmlControls.HtmlGenericControl hover_content = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hover_content.Attributes["class"] = "portfolio-hover-content";
System.Web.UI.HtmlControls.HtmlGenericControl i = new System.Web.UI.HtmlControls.HtmlGenericControl("i");
i.Attributes["class"] = "fa fa-2x";
createDiv1.ID = "createDiv" + n + 12;
createDiv1.Attributes["class"] = "col-md-4 col-sm-6 portfolio-item zoomIn animated";
createDiv1.Attributes["data-wow-duration"] = "1500ms";
createDiv1.Attributes["data-wow-delay"] = "100ms";
createDiv.Attributes["class"] = "portfolio-caption";
LinkButton a = new LinkButton();
a.Attributes["class"] = "portfolio-link";
a.Attributes["data-toggle"] = "modal";
//a.Click += new EventHandler(test);
//a.CommandArgument = item_name;
/* System.Web.UI.HtmlControls.HtmlGenericControl a = new System.Web.UI.HtmlControls.HtmlGenericControl("a");
a.Attributes["class"] = "portfolio-link";
a.Attributes["data-toggle"] = "modal";*/
System.Web.UI.HtmlControls.HtmlGenericControl h1 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h2 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h3 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h4 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h5 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
System.Web.UI.HtmlControls.HtmlGenericControl h6 = new System.Web.UI.HtmlControls.HtmlGenericControl("h4");
h1.Attributes["style"] = "color:black;float: left;width: 50%; ";
h1.InnerHtml = "Name &nbsp &nbsp :&nbsp &nbsp " + "item_name";
h2.Attributes["style"] = "color:black;float: left;width: 50%; ";
h2.InnerHtml = "color &nbsp &nbsp &nbsp &nbsp :&nbsp &nbsp " + "color";
h3.Attributes["style"] = "color:black;float: left;width: 50%; ";
h3.InnerHtml = "Weight &nbsp &nbsp: &nbsp &nbsp " + "weight";
h4.Attributes["style"] = "color:black;float: left;width: 50%; ";
h4.InnerHtml = "Quality &nbsp&nbsp: &nbsp " + "quality";
h5.Attributes["style"] = "color:black;float: left;width: 50%; ";
h5.InnerHtml = "Clarity &nbsp&nbsp: &nbsp " + "clarity";
h6.Attributes["style"] = "color:black;float: left;width: 50%; ";
h6.InnerHtml = "Origin &nbsp&nbsp: &nbsp " + "origin";
System.Web.UI.HtmlControls.HtmlGenericControl hyp = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
hyp.Attributes["class"] = "portfolio-link";
System.Web.UI.WebControls.Image tb = new System.Web.UI.WebControls.Image() { ID = "Image" + n + 10 };
tb.Height = 250;
tb.Width = 500;
tb.ImageUrl = "data:image/png;base64," /*+ Convert.ToBase64String(bytes)*/;
hover_content.Controls.Add(i);
a.Controls.Add(hover);
a.Controls.Add(hover_content);
a.Controls.Add(tb);
createDiv.Controls.Add(h1);
createDiv.Controls.Add(h2);
createDiv.Controls.Add(h3);
createDiv.Controls.Add(h4);
createDiv.Controls.Add(h5);
createDiv.Controls.Add(h6);
createDiv1.Controls.Add(a);
createDiv1.Controls.Add(createDiv);
// row.Controls.Add(createDiv1);
//section.Controls.Add(row);
divid1.Controls.Add(createDiv1);
n++;
}

Related

Unable to update the textbox values in the gridview[c# asp.net]

Getting old values in DataTable, I need new values which I updated. Please help me?
Update on button click
My code:
DataSet Ds = new DataSet();
string Query = "";
Query = "select Conv_0,Unit_0,Rate_0,Text_0,Barcode_0,Conv_1,Unit_1,Rate_1,Text_1,Barcode_1,Conv_2,Unit_2,Rate_2,Text_2,Barcode_2,Conv_3,Unit_3,Rate_3,Text_3,Barcode_3,Conv_4,Unit_4,Rate_4,Text_4,Barcode_4 from Prod_Unit where Code='" + a + "'";
Ds = SqlClass1.GetData_from_localhost(Query);
if (Ds.Tables[0].Rows.Count > 0)
{
dt.Columns.Clear();
dt.Rows.Clear();
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
// dt.Rows.Add();
for (int i = 0; i < Ds.Tables[0].Columns.Count; i++)
{
if (i >= 0 && i <= 4)
{
dt.Rows.Add();
dt.Rows[i]["NO"] = i + 1;
string unit = Ds.Tables[0].Rows[0]["Unit_" + i + ""].ToString();
DataSet temp = SqlClass1.GetData_from_localhost("select Unit FROM Units where Code=" + unit + "");
if (temp.Tables[0].Rows.Count > 0)
{
dt.Rows[i]["UNITS"] = temp.Tables[0].Rows[0][0].ToString();
}
dt.Rows[i]["CONVERSION"] = Ds.Tables[0].Rows[0]["Conv_" + i + ""].ToString();
dt.Rows[i]["BARCODE"] = Ds.Tables[0].Rows[0]["Barcode_" + i + ""].ToString();
dt.Rows[i]["RATE 1"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["RATE 2"] = Ds.Tables[0].Rows[0]["Rate_" + i + ""].ToString();
dt.Rows[i]["PRINT TEXT"] = Ds.Tables[0].Rows[0]["Text_" + i + ""].ToString();
}
}
if (i == 0)
{
// ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
// SetPreviousData();
}
}
ViewState["CurrentTable"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
SetPreviousData();// function to get previous data in
textbox
This is my binding function that I call in postback. How can I get updated values in ViewState["CurrentTable"] = dt;
on Update Button
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add("NO");
dt.Columns.Add("UNITS");
dt.Columns.Add("CONVERSION");
dt.Columns.Add("BARCODE");
dt.Columns.Add("RATE 1");
dt.Columns.Add("RATE 2");
dt.Columns.Add("PRINT TEXT");
dr = dt.NewRow();
dr["NO"] = 1;
dr["UNITS"] = string.Empty;
dr["CONVERSION"] = string.Empty;
dr["BARCODE"] = string.Empty;
dr["RATE 1"] = string.Empty;
dr["RATE 2"] = string.Empty;
dr["PRINT TEXT"] = string.Empty;
dt.Rows.Add(dr);
int rowIndex = 0;
for (int i = 0; i < GridView1.Rows.Count; i ++)
{
if (GridView1.Rows[rowIndex].RowType == DataControlRowType.DataRow)
{
DropDownList box1 = (DropDownList)GridView1.Rows[rowIndex].Cells[1].FindControl("DropDownList1");
TextBox box2 = (TextBox)GridView1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)GridView1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
TextBox box4 = (TextBox)GridView1.Rows[rowIndex].Cells[4].FindControl("TextBox4");
TextBox box5 = (TextBox)GridView1.Rows[rowIndex].Cells[5].FindControl("TextBox5");
TextBox box6 = (TextBox)GridView1.Rows[rowIndex].Cells[6].FindControl("TextBox6");
dt.Rows[i]["NO"] = i + 1;
dt.Rows[i]["Units"] = box1.Text;
dt.Rows[i]["Conversion"] = box2.Text;
dt.Rows[i]["Barcode"] = box3.Text;
dt.Rows[i]["Rate 1"] = box4.Text;
dt.Rows[i]["Rate 2"] = box5.Text;
dt.Rows[i]["Print Text"] = box6.Text;
dt.Rows.Add();
rowIndex++;
}
}
ViewState["CurrentTable"] = dt;
Save();//function for save
}

How do I auto generate content based on my database?

Good day. I have this content form but I do not know how to generate it automatically depending on the number of results from the database. I'm fairly new to ASP.Net
<asp:Content ID="Content1" ContentPlaceHolderID="AdminMain" runat="server">
<h1 class="container"><asp:Label runat="server" ID="PageTitle"></asp:Label></h1>
<div class="container">
<hgroup class="title">
<h1 class="post-title">
<asp:Label runat="server" ID="PostName"></asp:Label>
</h1>
<h6>Posted by <asp:Label runat="server" ID="AuthorName"></asp:Label> on <asp:Label runat="server" ID="PublushedDate"></asp:Label></h6>
</hgroup>
<div class="post-content">
<asp:Label runat="server" ID="PostContent"></asp:Label>
</div>
<div class="post-tags">
<h6>Categorized as:<asp:Label runat="server" ID="PostCategory"></asp:Label><br /></h6>
<h6>Tagged as:<asp:Label runat="server" ID="PostTag"></asp:Label></h6>
</div>
Here's what I'm going to populate it with:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
GetAllPosts();
}
}
private void GetAllPosts()
{
var sql = "SELECT p.*, t.Id as TagId, t.Name as TagName, " +
"t.UrlFriendlyName as TagUrlFriendlyName, u.Username FROM Posts p " +
"LEFT JOIN PostsTagsMap m ON p.Id = m.PostId " +
"LEFT JOIN Tags t ON t.Id = m.TagId " +
"INNER JOIN Users u ON u.Id = p.AuthorId";
SqlConnection SQLConn = new SqlConnection(con);
SqlCommand SQLComm = new SqlCommand(sql,SQLConn);
SqlDataAdapter SQLAd = new SqlDataAdapter(SQLComm);
DataTable dt = new DataTable();
SQLAd.Fill(dt);
foreach (DataRow row in dt.Rows)
{
PageTitle.Text = "All Posts";
PostName.Text = row["Title"].ToString();
AuthorName.Text = row["Username"].ToString();
PublishedDate.Text = Convert.ToDateTime(row["DatePublished"]).ToString();
PostContent.Text = row["Content"].ToString();
PostCategory.Text = "Events";
PostTag.Text = row["TagName"].ToString();
}
}
I appreciate the input and the help.
Webforms are terrible.
int i = 1;
PageTitle.Text = "All Posts";
foreach (DataRow row in dt.Rows)
{
Label newlabel = new Label();
PlaceHolder newplaceholder = new PlaceHolder();
newlabel.ID ="PostName" + i.ToString() + "</a>";
//html.Append(#"<hgroup class=""title"">");
//html.Append(#"<h1 class=""post-title"">");
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<hgroup class=""title""><h1 class=""post-title"">" + row["Title"].ToString() + "</h1>";
newlabel = new Label();
newlabel.ID = "AuthorName" + i.ToString();
html.Append(#"<h6>");
PanelPosts.Controls.Add(newlabel);
newlabel.Text ="Posted by: "+row["Username"].ToString()+" on ";
newlabel = new Label();
newlabel.ID = "PublishedDate" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = row["DatePublished"].ToString()+"</h6>";
html.Append("</hgroup>");
newlabel = new Label();
newlabel.ID = "PostContent" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<div class=""post-content""><h3>" + row["Content"].ToString() + "</h3><br /></div>";
newlabel = new Label();
newlabel.ID = "PostCategory" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<div class=""post-tags""><h6>Categorized as: " + "Category" + " <br/> </h6>";
newlabel = new Label();
newlabel.ID = "PostTag" + i.ToString();
PanelPosts.Controls.Add(newlabel);
newlabel.Text = #"<h6>Tagged as: <a href=""/archive/tag/{TagUrlFriendlyName}/"">"+row["TagName"].ToString()+"</div>";
PanelPosts.Controls.Add(new LiteralControl("<br />"));
i++;
}

Sorting table when used with literalControl ASP.net

I have a problem sorting this table. This was just a project that was pass unto me, and they want me to make some changes with the codes. Here's the code used to create table with data.
DataTable dt = common.get_entry(sname);
if (dt.Rows.Count != 0)
{
error_msg.Visible = false;
loader4.Visible = true;
}
else
{
loader4.Visible = false;
error_msg.Visible = true;
}
int maxsize = 10;
int rpage = Convert.ToInt32(p);
int startrow = ((rpage - 1) * maxsize) + 1;
int endrow = (rpage * maxsize);
if (dt.Rows.Count > 0)
{
int rcnt = dt.Rows.Count;
var tbl = view_tbl;
view_tbl.CssClass = "table table-hover table-bordered";
var maxpage = rcnt / maxsize;
var excess = rcnt % maxsize;
if (excess > 0) maxpage++;
TableHeaderRow r1 = new TableHeaderRow();
r1.TableSection = TableRowSection.TableHeader;
TableHeaderCell c2 = new TableHeaderCell();
c2.Controls.Add(new LiteralControl(""));
c2.Width = 45;
r1.Cells.Add(c2);
TableHeaderCell c8 = new TableHeaderCell();
c8.Controls.Add(new LiteralControl("Store Code"));
c8.Width = 120;
r1.Cells.Add(c8);
TableHeaderCell c3 = new TableHeaderCell();
c3.Controls.Add(new LiteralControl("Store Name"));
c3.Width = 250;
r1.Cells.Add(c3);
TableHeaderCell c4 = new TableHeaderCell();
c4.Controls.Add(new LiteralControl("Line of Business"));
c4.Width = 150;
r1.Cells.Add(c4);
TableHeaderCell c5 = new TableHeaderCell();
c5.Controls.Add(new LiteralControl("Visitors Name"));
c5.Width = 170;
r1.Cells.Add(c5);
TableHeaderCell c7 = new TableHeaderCell();
c7.Controls.Add(new LiteralControl("Date of Visit"));
c7.Width = 100;
r1.Cells.Add(c7);
TableHeaderCell c9 = new TableHeaderCell();
c9.Controls.Add(new LiteralControl("Added Date"));
c9.Width = 170;
r1.Cells.Add(c9);
TableHeaderCell c6 = new TableHeaderCell();
c6.Controls.Add(new LiteralControl("Action"));
c6.Width = 100;
r1.Cells.Add(c6);
tbl.Rows.Add(r1);
for (int cnt = startrow - 1; cnt < rcnt && cnt < endrow; cnt++)
{
TableRow r = new TableRow();
String sn = dt.Rows[cnt][3].ToString();
String lo = dt.Rows[cnt][5].ToString();
String id = dt.Rows[cnt][0].ToString();
String vn = dt.Rows[cnt][4].ToString();
String ed = dt.Rows[cnt][1].ToString();
String da = dt.Rows[cnt][6].ToString();
String sc = dt.Rows[cnt][7].ToString();
string[] edd = new string[2];
edd = ed.Split(' ');
ed = edd[0] + " " + dt.Rows[cnt][2].ToString();
TableCell cc1 = new TableCell();
cc1.Controls.Add(new LiteralControl("<input class='check_box' rel='" + id + "'type='checkbox'> <a href='#'><i class='icon-star-empty'></i></a>"));
r.Cells.Add(cc1);
TableCell cc2 = new TableCell();
cc2.Controls.Add(new LiteralControl("<strong>" + sc + "</strong>"));
r.Cells.Add(cc2);
TableCell cc7 = new TableCell();
cc7.Controls.Add(new LiteralControl("<strong>" + sn + "</strong>"));
r.Cells.Add(cc7);
TableCell cc3 = new TableCell();
cc3.Controls.Add(new LiteralControl("<strong>" + lo + "</strong>"));
r.Cells.Add(cc3);
TableCell cc4 = new TableCell();
cc4.Controls.Add(new LiteralControl("<strong>" + vn + "</strong>"));
r.Cells.Add(cc4);
TableCell cc5 = new TableCell();
cc5.Controls.Add(new LiteralControl("<strong>" + ed + "</strong>"));
r.Cells.Add(cc5);
TableCell cc8 = new TableCell();
cc8.Controls.Add(new LiteralControl("<strong>" + da + "</strong>"));
r.Cells.Add(cc8);
TableCell cc6 = new TableCell();
cc6.Controls.Add(new LiteralControl("<a data-target='#viewentry' data-toggle='modal' class='btn btn-mini btn-success view_btn' rel='" + id + "'><i class='icon-th-list icon-white'></i> View</a> <a data-target='#delentry' data-toggle='modal' class='btn btn-mini btn-danger del_btn hidden' rel='" + id + "'><i class='icon-trash icon-white'></i></a>"));
r.Cells.Add(cc6);
tbl.Rows.Add(r);
}
var dpagi = pagi;
LiteralControl ul = new LiteralControl();
ul.Text = "<ul>";
for (int cnt5 = 1; cnt5 <= maxpage; cnt5++)
{
if (cnt5 == rpage)
ul.Text = ul.Text + "<li class='disabled'><a href='StoreVisitView.aspx?page=" + cnt5 + "'>" + cnt5 + "</a></li>";
else
ul.Text = ul.Text + "<li><a href='StoreVisitView.aspx?page=" + cnt5 + "' class='pgn'>" + cnt5 + "</a></li>";
}
ul.Text = ul.Text + "</ul>";
dpagi.Controls.Add(ul);
pagi.CssClass = "pagination";
loader4.Visible = false;
I have no idea how to sort this because the table was made thru literalcontrol and etc..
If you want to sort the data on server side I suggest using a DataView to sort it. After it's sorted you can render it manually as you do now (iterate through DataView's rows) or bind a DataGrid to it.
DataView view = new DataView(dt);
view.Sort = "Column ASC, Column2 ASC";
// Now iterate through rows of dataview
foreach (DataRowView row in view) {...}
Other option is to sort it on client side using some sorting plugin.
For example Tablesorter seems pretty easy to use. Or just google "sort table javascript"...

Adding eventHandler in each label from the code behind for Pager numbers

My page structure is like this:
<div style="BORDER-BOTTOM: black 1px solid; BORDER-LEFT: black 1px solid; OVERFLOW-Y: auto; WIDTH: 100%; ; HEIGHT: expression(document.body.clientHeight-270); BORDER-TOP: black 1px solid; BORDER-RIGHT: black 1px solid"id="divGrid">
<asp:datalist id="dlResults" runat="server" Width="100%" CellSpacing="0" CellPadding="0" RepeatDirection="Vertical">
...
</asp:datalist>
</div>
<table class="bodytext8pt" border="0" width="100%">
<tr>
<td><asp:panel id="pnlPager" Runat="server" CssClass="GridFooter"></asp:panel></td>
</tr>
</table>
In the code behind pager is build by the BuildPager function:
private void BuildPager(DataTable dt)
{
pnlPager.Controls.Clear();
Label l = new Label();
l.Text = " (" + pgResults.PageCount.ToString("#,##0") + " pages, " + dt.Rows.Count.ToString("#,##0") + " records ) ";
pnlPager.Controls.Add(l);
for (int i = 0; i < pnlPager.Controls.Count; i++)
{
if (pnlPager.Controls[i].ToString() == "System.Web.UI.WebControls.DataGridLinkButton")
{
try
{
LinkButton c = (LinkButton) pnlPager.Controls[i];
c.CssClass = "GridFooter";
}
catch (Exception ex) { }
}
}
pnlPager.Attributes.Add("class", "GridFooter");
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
LinkButton lb = null;
lb = new LinkButton();
lb.Text = "Previous";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) - 1);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
lb = new LinkButton();
lb.Text = "First";
lb.Attributes.Add("OnClick", "goPage(0);");
lb.CommandArgument = Convert.ToString(0);
if (pgResults.CurrentPageIndex == 0)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);
pnlPager.Controls.AddAt(0, lb);**
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(0, l);
//Build the numeric links..
for(int i=0; i< pgResults.PageCount; i++)
{
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = Convert.ToString(i+1);
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(i) + ");");
lb.CommandArgument = Convert.ToString(i);
if (pgResults.CurrentPageIndex == i)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
**lb.Click += new EventHandler(Pager_Click);**
}
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
}
//End of numeric links
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Next";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.CurrentPageIndex) + 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
lb = new LinkButton();
lb.Text = "Last";
lb.Attributes.Add("OnClick", "goPage(" + Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1) + ");");
lb.CommandArgument = Convert.ToString(Convert.ToInt32(pgResults.PageCount) - 1);
if (pgResults.CurrentPageIndex == pgResults.PageCount - 1)
{
lb.Enabled = false;
lb.Style.Add("cursor", "default");
}
else
{
lb.Attributes.Add("class", "GridFooter");
}
**lb.Click += new EventHandler(Pager_Click);**
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, lb);
l = new Label();
l.Width = Unit.Pixel(5);
pnlPager.Controls.AddAt(pnlPager.Controls.Count - 1, l);
}
Number labels are added with the clicking events:
private void Pager_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton) sender;
pgResults.CurrentPageIndex = Convert.ToInt32(lb.CommandArgument);
BindList(false);
}
But when i am clicking these numbers or 'next','previous' this pager_click is not firing.
Have i added eventhandlers in a correct way.
Please suggest
Thanks
Found out the reasion.
Dynamically created controls are lost on the page load,
so we need to again load them in postback.

Find radio button control in nested repeater control in asp.net?

I have two repeater controls one inside other and in inner repeater there is placeholder in which radio button is dynamically generated. I want to find the radio button control to check whether it is checked or not? I want above all function to be performed in button submit/click event defined in the code?
if (!Page.IsPostBack)
{
//1) Load SomeDatatable from Database somehow
// Just for testing : replace with query to DB
strqry = "select * from Quiz_tblQsnsLimitMaster where Qsnexamname='" + Request.QueryString["QsnEname"].ToString() + "'";
SqlDataAdapter adp = new SqlDataAdapter(strqry, sCon);
DataSet ds = new DataSet();
try
{
adp.Fill(ds);
int total = ds.Tables[0].Rows.Count;
for (int i = 0; i < total; i++)
{
string QuesID = ds.Tables[0].Rows[i].ItemArray[1].ToString();
//SubName = ds.Tables[0].Rows[i].ItemArray[3].ToString();
DataSet oDs = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_OnlineTest_QuestionsWithOptions_Get", QuesID);
SomeDatatable.Merge(oDs.Tables[0]);
}
removeDuplicatesRows(SomeDatatable);
System.Data.DataColumn newColumn = new System.Data.DataColumn("ContentIndex", typeof(System.String));
newColumn.DefaultValue = "0";
SomeDatatable.Columns.Add(newColumn);
for (int i = 0; i < Math.Ceiling((decimal)SomeDatatable.Rows.Count); i++)
SomeDatatable.Rows[i]["ContentIndex"] = i + 1;
}
catch
{
}
////2) Create a dummy data source for the tab repeater using a list of anonymous types
List<object> TabList = new List<object>();
//BindSubject();
for (int I = 0; I < Math.Ceiling((decimal)SomeDatatable.Rows.Count / (decimal)ContentPerTab); I++)
{
TabList.Add(new { TabIndex = I });
}
TabRepeater.ItemDataBound += TabRepeater_ItemDataBound;
TabRepeater.DataSource = TabList;
TabRepeater.DataBind();
//TablLinkRepeater.DataSource = TabList;
//TablLinkRepeater.DataBind();
//}
}
public void removeDuplicatesRows(DataTable dt)
{
SomeDatatable = dt.DefaultView.ToTable(true, "QuestionId");
}
protected void TabRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
int TabIndex = -1;
int.TryParse(DataBinder.Eval(e.Item.DataItem, "TabIndex").ToString(), out TabIndex);
//Copy Content Rows from SomeDatable that belong to this tab
DataTable Dt = SomeDatatable.Clone();
for (Int32 i = TabIndex * ContentPerTab; i <= (TabIndex + 1) * ContentPerTab - 1; i++)
{
if (i >= SomeDatatable.Rows.Count) break;
Dt.ImportRow(SomeDatatable.Rows[i]);
}
// Find the content repeater in this item and use the new datatable as source
Repeater ContentRepeater = (Repeater)e.Item.FindControl("ContentRepeater");
ContentRepeater.ItemDataBound += ContentRepeater_ItemDataBound;
ContentRepeater.DataSource = Dt;
ContentRepeater.DataBind();
}
}
// This handler might be needed for content repeater, included just for testing
protected void ContentRepeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem)
{
//Read coulmn from Datarow
int ContentIndex = -1;
int.TryParse(DataBinder.Eval(e.Item.DataItem, "ContentIndex").ToString(), out ContentIndex);
var parsed = "'" + HttpUtility.ParseQueryString(DataBinder.Eval(e.Item.DataItem, "QuestionID").ToString()) + "'";
//Add Question
DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", Convert.ToString(parsed));
if (ds.Tables.Count > 0)
{
if (ds.Tables[0].Rows.Count > 0)
{
int iCnt = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Table tblQsn = new Table();
//.....Begin Text Qsn Creation.....//
tblQsn.Width = new Unit("98%");
TableRow trQsn = new TableRow();
iRowCounter++;
trQsn.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsn = new TableCell();
TableCell tcQsnSNo = new TableCell();
tcQsn.CssClass = "Label";
tcQsn.BackColor = System.Drawing.Color.Gainsboro;
tcQsn.Font.Bold = true;
tcQsn.Font.Size = 12;
tcQsn.Text = ds.Tables[0].Rows[i][1].ToString();
tcQsn.Width = Unit.Percentage(99.5);
iCellCounter++;
tcQsn.ID = "Cell_" + iCellCounter.ToString();
tcQsnSNo.CssClass = "Label";
tcQsnSNo.Attributes.Add("valign", "top");
tcQsnSNo.BackColor = System.Drawing.Color.Gainsboro;
tcQsnSNo.Font.Bold = true;
tcQsnSNo.Width = Unit.Percentage(0.5);
iCellCounter++;
tcQsnSNo.ID = "Cell_" + iCellCounter.ToString();
iCnt++;
tcQsnSNo.Text = ContentIndex.ToString() + ".";
trQsn.Cells.Add(tcQsnSNo);
trQsn.Cells.Add(tcQsn);
tblQsn.Rows.Add(trQsn);
int rcnt = i;
int iOptCnt = 0;
string sStatus = "N";
while ((rcnt >= 0) && (rcnt < ds.Tables[0].Rows.Count))
{
if (ds.Tables[0].Rows[rcnt][2].ToString() == ds.Tables[0].Rows[i][2].ToString())
{
if (sStatus == "N")
{
sStatus = "Y";
}
TableRow trQsnOpt = new TableRow();
iRowCounter++;
trQsnOpt.ID = "Row_" + iRowCounter.ToString();
TableCell tcQsnOpt = new TableCell();
tcQsnOpt.CssClass = "Label";
iCellCounter++;
tcQsnOpt.ID = "Cell_" + iCellCounter.ToString();
tcQsnOpt.Attributes.Add("valign", "top");
tcQsnOpt.VerticalAlign = VerticalAlign.Middle;
TableCell tcQsnOptSNo = new TableCell();
tcQsnOptSNo.CssClass = "Label";
iCellCounter++;
tcQsnOptSNo.ID = "Cell_" + iCellCounter.ToString();
iOptCnt++;
RadioButton oRbOptions = new RadioButton();
oRbOptions.GroupName = ds.Tables[0].Rows[rcnt][2].ToString() + "_Group";
oRbOptions.Text = ds.Tables[0].Rows[rcnt][3].ToString().Trim();
oRbOptions.Font.Size = 11;
iRbTCounter++;
oRbOptions.ID = ds.Tables[0].Rows[i][0].ToString() + "_" + ds.Tables[0].Rows[rcnt][2].ToString() + "_" + "Option" + iOptCnt.ToString() + "_" + iRbTCounter.ToString();
//oRbOptions.Enabled = false;
//if (ds.Tables[0].Rows[i][2].ToString() == "Option" + iRbTCounter.ToString())
//{
// oRbOptions.Checked = true;
//}
oRbOptions.InputAttributes.Add("data-info", Convert.ToString(ContentIndex));
oRbOptions.CssClass = "Label";
tcQsnOpt.Controls.Add(oRbOptions);
tcQsnOptSNo.Text = iOptCnt.ToString() + ".";
trQsnOpt.Cells.Add(tcQsnOptSNo);
trQsnOpt.Cells.Add(tcQsnOpt);
rcnt++;
//.....Add Option Image.....//
tblQsn.Rows.Add(trQsnOpt);
}
else
break;
}
i = rcnt - 1;
PlPreview = (PlaceHolder)e.Item.FindControl("PlPreview");
PlPreview.Controls.Add(tblQsn);
}
}
}
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
SubmitQuestion();
}
public void SubmitQuestion()
{
for (int c = 0; c < SomeDatatable.Rows.Count; c++)
{
string sQsnOptId = SomeDatatable.Rows[c]["QuestionID"].ToString();
DataSet ds = SqlHelper.ExecuteDataset(sCon, "Ps_Quiz_QuestionsWithOptions_Get", "'" + sQsnOptId + "'");
for (int i = 1; i <= 4; i++)
{
string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i-1][2].ToString() + "_" + "Option" + i + "_" + i;
Repeater rpt = (Repeater)TabRepeater.NamingContainer.FindControl("ContentRepeater");
RadioButton rbt = (RadioButton)rpt.NamingContainer.FindControl(rdboption);
if (rbt.Checked)
{
strqry = "update Quiz_tblOnlineTest_Detail set UserAns='Option" + i + "', DeletionStatus='A' where CreationLogInId='AMITSAMBYAL#HOTMAIL.COM' and ExamName='" + Request.QueryString["QsnEname"].ToString() + "' and QsnId=" + Session["quesid"].ToString() + "";
cmd = new SqlCommand(strqry, con);
try
{
cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
break;
}
}
}
}
}
I found the answer. Thanks
foreach (RepeaterItem repeater in TabRepeater.Items)
{
Repeater repeater1 = (Repeater)repeater.FindControl("ContentRepeater");
foreach (RepeaterItem repItem in repeater1.Items)
{
for (int i = 1; i <= 4; i++)
{
string rdboption = ds.Tables[0].Rows[c][0].ToString() + "_" + ds.Tables[0].Rows[i - 1][2].ToString() + "_" + "Option" + i + "_" + i;
PlaceHolder PlPreview = (PlaceHolder)repItem.FindControl("PlPreview");
rbt = (RadioButton)PlPreview.FindControl(rdboption);
if (rbt.Checked)
{
// statement
}
}
}
}
}
}

Resources