asp.net grid view hyper link pass values to new window - asp.net

hai guys here is my question please help me
I have a gridview with hyperlink fields here my requirement is if I click on hyperlink of particular row I need to display that particular row values into other page in that page user will edit record values after that if he clicks on update button I need to update that record values and get back to previous home page. if iam clicking hyper link in first window new window should open with out any url tool bar and minimize close buttons like popup modular Ajax ModalPopUpExtender but iam un able to ge that that one please help me
the fillowing code is defalut.aspx
<head runat="server">
<title>PassGridviewRow values </title>
<style type="text/css">
#gvrecords tr.rowHover:hover
{
background-color:Yellow;
font-family:Arial;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView runat="server" ID="gvrecords" AutoGenerateColumns="false"
HeaderStyle-BackColor="#7779AF" HeaderStyle-ForeColor="White" DataKeyNames="UserId" RowStyle-CssClass="rowHover">
<Columns>
<asp:TemplateField HeaderText="Change Password" >
<ItemTemplate>
<a href ='<%#"UpdateGridviewvalues.aspx?UserId="+DataBinder.Eval(Container.DataItem,"UserId") %>'> <%#Eval("UserName") %> </a>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="Email" HeaderText="Email" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
following code default.aspx.cs code
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridview();
}
}
protected void BindGridview()
{
SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=testdb1");
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
con.Close();
DataSet ds = new DataSet();
da.Fill(ds);
gvrecords.DataSource = ds;
gvrecords.DataBind();
}
}
this is updategridviewvalues.aspx
<head runat="server">
<title>Update Gridview Row Values</title>
<script type="text/javascript">
function Showalert(username) {
alert(username + ' details updated successfully.');
if (alert) {
window.location = 'Default.aspx';
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr>
<td colspan="2" align="center">
<b> Edit User Details</b>
</td>
</tr>
<tr>
<td>
User Name:
</td>
<td>
<asp:Label ID="lblUsername" runat="server"/>
</td>
</tr>
<tr>
<td>
First Name:
</td>
<td>
<asp:TextBox ID="txtfname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<asp:TextBox ID="txtlname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:
</td>
<td>
<asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnUpdate" runat="server" Text="Update" onclick="btnUpdate_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" onclick="btnCancel_Click"/>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
and my updategridviewvalues.aspx.cs code is follows
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class UpdateGridviewvalues : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=.;Integrated Security=SSPI;Initial Catalog=testdb1");
private int userid=0;
protected void Page_Load(object sender, EventArgs e)
{
userid = Convert.ToInt32(Request.QueryString["UserId"].ToString());
if(!IsPostBack)
{
BindControlvalues();
}
}
private void BindControlvalues()
{
con.Open();
SqlCommand cmd = new SqlCommand("select * from UserDetails where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
con.Close();
DataSet ds = new DataSet();
da.Fill(ds);
lblUsername.Text = ds.Tables[0].Rows[0][1].ToString();
txtfname.Text = ds.Tables[0].Rows[0][2].ToString();
txtlname.Text = ds.Tables[0].Rows[0][3].ToString();
txtemail.Text = ds.Tables[0].Rows[0][4].ToString();
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = new SqlCommand("update UserDetails set FirstName='" + txtfname.Text + "',LastName='" + txtlname.Text + "',Email='" + txtemail.Text + "' where UserId=" + userid, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
int result= cmd.ExecuteNonQuery();
con.Close();
if(result==1)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:Showalert('"+lblUsername.Text+"')", true);
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("~/Default.aspx");
}
}
My requirement is new window should come like pop window as new window with out having url and close button
my database tables is
ColumnName DataType
-------------------------------------------
UserId Int(set identity property=true)
UserName varchar(50)
FirstName varchar(50)
LastName varchar(50)
Email Varchar(50)

There are a host of options in the jQuery field:
jQueryUI's dialog
Wijmo's Dialog
SimpleModal
If you'd already using jQuery, jQueryUI's option may be a good fit. Wijmo is also jQueryUI compatible/friendly (use the same theme CSS class names and patterns), so it's also a good fit.
So it kind of depends on what you want. Something very simple - maybe jQueryUI. Flashy/pretty -- SimpleModal. More complex but jQueryUI-friendly - Wijmo.

Related

SQL Database not working with ASP.NET web form

I am designing a web form on Visual Studio 2010 using ASP.NET framework. I am using SQL Database to connect my web form to the server, but there are some errors which I can't resolve?
Any help would be greatly appreciated.
I am trying to run a web form that connects to the database which when data entered into the form, stores the information into the server for the user to login later
The following is the code for the web form.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(#"Data Source=.\SQLEXPRESS;AttachDbFilename=#"C:\Users\Kapalmeet Singh\Desktop\WebSite1\App_Data\Database.mdf";Integrated Security=True;User Instance=True")
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Table1 values('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";
cmd.ExecuteNonQuery();
con.Close();
}
}
And here's the code for the web form
<%# 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>
<style type="text/css">
.style1
{
width: 100%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<table class="style1">
<tr>
<td>
First Name</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
User Name</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password</td>
<td>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</td>
</tr>
</table>
<div>
</div>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
style="margin-left: 192px" Text="Submit" />
</form>
</body>
</html>
If your creating a registration it would be like this. Change the names for these the way they are called in your database and for the other ones to. (#FirstName, #Surname, #Password, #Username,)
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["YOUR_CONNECION_STRING_HERE"].ConnectionString);
conn.Open();
string insertQuery = "insert into Table1(FirstName, Surname, Username, Password) " +
"values(#FirstName, #Surname, #Password, #Username,)";
SqlCommand com = new SqlCommand(insertQuery, conn);
com.Parameters.AddWithValue("#FirstName", Textbox1.Text);
com.Parameters.AddWithValue("#Surname", Textbox2.Text);
com.Parameters.AddWithValue("#Password", Textbox3.Text);
com.Parameters.AddWithValue("#Username", Textbox4.Text);
//This actually executes the query with the given values above.
com.ExecuteNonQuery();
conn.Close();
Response.Redirect("Name_Of_Any_Page_Thats_On_Your_Site.aspx");
Response.Write("there was a problem with your registration");
}
catch (Exception problem)
{
//throw Exception ;
Response.Write("Error Message: " + problem.ToString());
throw;
}

how to add null values in dropdownlist to sql server by using asp.net

I have created two table in SQL server they are:
TblStudent:
TblProvince:
In Asp.net Design Form:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="StudentName"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtStudentName" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="Province"></asp:Label>
</td>
<td>
<asp:DropDownList ID="ddlProvince" runat="server"></asp:DropDownList>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
In Asp.net Back-End :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class Default4 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bound data from TblProvince to ddlProvince
using (SqlConnection con = DBManager.getConnection())
{
string sql = #"Select ProvinceId, Province From TblProvince";
con.Open();
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
da.Fill(ds, "dtProvince");
ddlProvince.DataSource = ds.Tables["dtProvince"];
ddlProvince.DataValueField = "ProvinceId";
ddlProvince.DataTextField = "Province";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("Please Select", "NULL"));
}
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
using (SqlConnection con = DBManager.getConnection())
{
string sql = #"Insert Into TblStudent(Name, ProvinceId) Values(#Name, #ProvinceId)";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("#Name", SqlDbType.NChar).Value = txtStudentName.Text;
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
Question:
When I do not select in ddlProvince why when u save it to database it shows an error Input string was not in a correct format.
It throws "Input string was not in a correct format" error because you are trying to convert null to Int.
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = Convert.ToInt32(ddlProvince.SelectedValue);
You can check whether SelectedValue is null before converting it to Int as below.
cmd.Parameters.Add("#ProvinceId", SqlDbType.Int).Value = (ddlProvince.SelectedValue == null) ? null : Convert.ToInt32(ddlProvince.SelectedValue)

Take parameter from listview

i have this code:
protected void Button1_Click(object sender, EventArgs e)
{
System.Data.SqlClient.SqlConnection sc = new System.Data.SqlClient.SqlConnection(GetConnectionString());
{
System.Data.SqlClient.SqlCommand cmd;
sc.Open();
cmd = new System.Data.SqlClient.SqlCommand("SET IDENTITY_INSERT Zapas OFF INSERT INTO Zapas (Zapas.Sezona,.....)SELECT Zapas.Sezona,... FROM Zapas WHERE ID_zapas=#ID;", sc);
cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("#ID", System.Data.SqlDbType.Text)).Value = (TextBox)ListView1.FindControl("box1");
cmd.ExecuteNonQuery();
sc.Close();
Response.Redirect("~/Zapasy_seznam.aspx");
}
}
I need take value ID from listview, but with this code I have this error:
...expects the parameter '#ID', which was not supplied....
This part of my listview...
<asp:ListView ID="ListView1" runat="server" DataKeyNames="ID_zapas" DataSourceID="SqlDataSource1" style="text-align: center ">
<AlternatingItemTemplate>
<tr style="background-color: #e9ffe9;color: #284775;text-align:center">
<td>
<asp:TextBox ID="box1" runat="server" Text='<%# Eval("ID_zapas") %>' Visible="false" />
...
<td style="width:50px;background-color:white">
<asp:LinkButton ID="Button1" runat="server" OnClick="Button1_Click" Visible='<%# HttpContext.Current.User.IsInRole("admin") %>' CausesValidation="False" OnClientClick="javascript: return confirm('Opravdu chcete zápas zkopírovat?');">
<asp:Image ID="Image2" runat="server" ImageUrl="~/Icons/copy.png" Width="29px" Height="29px" ToolTip="Zkopírovat zápas" />
</asp:LinkButton>
</td>
</tr>
</AlternatingItemTemplate>
Have you some idea?
As per comments, please try this:
using System.Data.SqlClient;
protected void Button1_Click(object sender, EventArgs e)
{
var box1 = (TextBox)((LinkButton)sender).Parent.FindControl("box1");
using (var sc = new SqlConnection(GetConnectionString()))
{
using (var cmd = sc.CreateCommand())
{
sc.Open();
cmd.CommandText = "SET IDENTITY_INSERT Zapas OFF INSERT INTO Zapas (Zapas.Sezona,.....)SELECT Zapas.Sezona,... FROM Zapas WHERE ID_zapas=#ID;";
cmd.Parameters.AddWithValue("#ID", box1.Text);
cmd.ExecuteNonQuery();
sc.Close();
Response.Redirect("~/Zapasy_seznam.aspx");
}
}
}
When using data-aware controls such as a ListView here, the controls are created with automatic unique IDs per data item. This means that you can have more than 1 of the same control (such as "box1" in this case) within the page that should be referenced by the data item (((LinkButton)sender).Parent which is ListViewDataItem, representing the row).
ListViewDataItem.FindControl will find controls of a specific server ID within its own child scope, allowing you to get values for controls within the same row of the button that is being clicked.

select index of listView is not working in ASP.NET

I am new to ASP.NET, I am making a search box in my application.
For example: if a user enters "abc" in the textbox, then the textbox will fetch data from the database which starts with "abc". I am passing this data to DataTable.
It works properly,
Here is my code snippet:
DataTable result = new DataTable();
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
Now I want to retrieve all this data in my <div> tag. so i tried using asp:ListView,
here is my code snippet,
it works properly, but now i want to navigate to new page when user select any row of listView, but i am unable to select any row..
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
</div>
</ItemTemplate>
Thanks In Advance !!
Any help will be appreciated.
EDIT:(SearchBox.aspx.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
public partial class SearchBox : System.Web.UI.Page
{
string connString;
DataTable result = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{ }
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
SqlConnection conn = new SqlConnection(connString);
conn.Open();
string query = string.Format("SELECT DISTINCT Scrip FROM dbo.SearchBoxData where Scrip Like '{0}%'", TextBox1.Text);
SqlCommand cmd = new SqlCommand(query, conn);
result.Load(cmd.ExecuteReader());
conn.Close();
lvwItems.DataSource = result;
lvwItems.DataBind();
}
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("NextPage.aspx?id=" + lablId.Text, false);
}
}
(SearchBox.aspx)
<%# Page Language="C#" AutoEventWireup="true" CodeFile="SearchBox.aspx.cs" Inherits="SearchBox" %>
<!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>
</div>
<asp:TextBox ID="TextBox1" runat="server" Height="30px" Width="179px"
OnTextChanged="TextBox1_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Go"
Width="62px" />
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="false" runat="server" Text='<%#Eval("Scrip") %>'/>
</div>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:krunal_DBConnectionString2 %>"
SelectCommand="SELECT * FROM [SearchBoxData]"></asp:SqlDataSource>
</form>
</body>
</html>
You have to add a SELECT button in the ItemTemplate, see the complete working code.
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<%# Eval("Scrip")%>
<asp:LinkButton ID="SelectButton" runat="server" CommandName="Select" Text="Select" />
</ItemTemplate>
</asp:ListView>
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("CONTROL_ID");
}
Thanks
Deepu
Probably you will have to add the event for Selected index change:
<asp:ListView ID="lvwItems" runat="server" ItemPlaceholderID="plhItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging">
In code behind you can get the selected row item like below.
Also you can place a label or hidden field so that you can get some data from those control and pass it in to the next page.. (might be id or something).
<asp:ListView ID="lvwItems" OnSelectedIndexChanging="lvwItems_SelectedIndexChanging"
runat="server" ItemPlaceholderID="plhItems">
<LayoutTemplate>
<div>
<asp:PlaceHolder ID="plhItems" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<div>
<%# Eval("Scrip")%>
<asp:Label ID="lablId" visible="flase" runat="server" Text='<%#Eval("Id") %>' />
</div>
</ItemTemplate>
</asp:ListView>
void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Label lablId = (Label)item.FindControl("lablId");
if (String.IsNullOrEmpty(lablId.Text))
{
Response.Redirect("page.aspx?id="+lablId.Text,false);
}
}
Thanks
Deepu
Here you go,
Also remove the OnClick="callMethod" from the button. Since you have SelectedIndex method no need to have the onClick method.
protected void lvwItems_SelectedIndexChanging(Object sender, ListViewSelectEventArgs e)
{
ListViewItem item = (ListViewItem)lvwItems.Items[e.NewSelectedIndex];
Button btn = (Button)item.FindControl("btn1");
if(btn != null)
{
var buttonText = btn.Text;
}
}
Hope this helps
Thanks
Deepu

Background color to comment

I am using ASP.NET repeater function. Its working fine. But here all the comments are coming one by one without any separation. I need each comment to be placed in a separate box and to add back ground color to that box. Just like in wordpress comment page how the comments are separated. My complete code follows please tell me how to separate each comment.
comments.aspx
<html>
<body>
<form id="form1" runat="server">
<asp:Repeater id="Repeater1" runat="server" >
<HeaderTemplate>
<table border=1>
</HeaderTemplate>
<ItemTemplate>
<tr>
<%# DataBinder.Eval(Container.DataItem, "Name") %> </br>
<%# DataBinder.Eval(Container.DataItem, "Val4") %> </br></br></br>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
<br />
<br />
Name*:
<asp:TextBox ID="tb_name" runat="server" placeholder="Your Name"></asp:TextBox>
<br />
<br />
Email*:
<asp:TextBox ID="tb_email" runat="server"></asp:TextBox>
<br />
<br />
Website :
<asp:TextBox ID="tb_website" runat="server"></asp:TextBox>
<br />
<br />
<asp:TextBox ID="tb_comment" runat="server" Height="114px" TextMode="MultiLine"
Width="322px"></asp:TextBox>
<br />
<br />
<br />
<asp:CheckBox ID="cb_notify" runat="server"
Text="Notify me of followup comments via e-mail." />
<br />
<br />
<br />
<asp:Button ID="Submit" runat="server" onclick="Submit_Click"
Text="Post Comment" />
</form>
</body>
</html>
comments.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Odbc;
using System.Collections;
public partial class comment : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=softmail;" + "UID=root;" + "PASSWORD=********;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
OdbcCommand cmd = new OdbcCommand("Select name, email, website, comments from awm_comments", MyConnection);
OdbcDataReader dr = cmd.ExecuteReader();
if (dr.HasRows == false)
{
throw new Exception();
}
ArrayList values = new ArrayList();
while (dr.Read())
{
if (!IsPostBack)
{
string a = dr[0].ToString();
string b = dr[1].ToString();
string c = dr[2].ToString();
string d = dr[3].ToString();
values.Add(new PositionData(a, b, c, d));
Repeater1.DataSource = values;
Repeater1.DataBind();
}
}
}
protected void Submit_Click(object sender, EventArgs e)
{
string name = tb_name.Text;
string email = tb_email.Text;
string website = tb_website.Text;
string comment = tb_comment.Text;
string MyConString = "DRIVER={MySQL ODBC 3.51 Driver};" + "SERVER=localhost;" + "DATABASE=softmail;" + "UID=root;" + "PASSWORD=******;" + "OPTION=3";
OdbcConnection MyConnection = new OdbcConnection(MyConString);
OdbcCommand cmd = new OdbcCommand("INSERT INTO awm_comments(name, email, website, comments, notify)VALUES(?, ?, ?, ?, ?)", MyConnection);
cmd.Parameters.Add("#email", OdbcType.VarChar, 255).Value = name;
cmd.Parameters.Add("#alternate_email", OdbcType.VarChar, 255).Value = email;
cmd.Parameters.Add("#ipaddr", OdbcType.VarChar, 255).Value = website;
cmd.Parameters.Add("#security_question", OdbcType.VarChar, 255).Value = comment;
if (cb_notify.Checked == true)
{
int not = 1;
cmd.Parameters.Add("#security_question", OdbcType.Int, 11).Value = not;
}
else if (cb_notify.Checked == false)
{
int not = 0;
cmd.Parameters.Add("#security_question", OdbcType.Int, 11).Value = not;
}
MyConnection.Open();
cmd.ExecuteNonQuery();
MyConnection.Close();
Response.Redirect("comment.aspx");
}
public class PositionData
{
private string name;
private string ticker;
private string val3;
private string val4;
public PositionData(string name, string ticker, string val3, string val4)
{
this.name = name;
this.ticker = ticker;
this.val3 = val3;
this.val4 = val4;
}
public string Name
{
get
{
return name;
}
}
public string Ticker
{
get
{
return ticker;
}
}
public string Val3
{
get
{
return val3;
}
}
public string Val4
{
get
{
return val4;
}
}
}
}
Output
I would make such a thing like this
<ItemTemplate>
<div style="padding: 10px; margin: 5px; border: 1px dotted black; background: yellow;">
<%# DataBinder.Eval(Container.DataItem, "Name") %> </br>
<%# DataBinder.Eval(Container.DataItem, "Val4") %>
</div>
</ItemTemplate>
With this approach you can leave Footer and Header tempaltes.
Style div as you like.
You can use a SeparatorTemplate (see: http://msdn.microsoft.com/en-us/library/c012haty(v=vs.71).aspx ) to add some markup for separating the comments.
If you want alternating colors etc., likewise you can use the AlternatingItemTemplate to change the colours for odd/even rows. Add different CSS classes to the TR and use your CSS for the colors.
(You might want to get rid of the table completely and use div's instead, but that is my personal taste)
I think you’re missing some HTML. I suspect your ASP.Net code is outputting HTML like this:
<table border=1>
<tr>
Name </br>
Val4 </br></br></br>
</tr>
</table>
You need table cell tags in there for the HTML to be valid:
<table border=1>
<tr>
<td>Name</td>
<td>Val4</td>
</tr>
</table>
And to make it look like you describe:
<table border=1 cellpadding=5 cellspacing=10>
<tr>
<td style="background-color: #ccc;">Name</td>
<td style="background-color: #ccc;">Val4</td>
</tr>
</table>

Resources