Submit form fails - asp.net

I have a page that which is an exam page,it selects all MCQ questions with their answers...
and in server side page load i add div.innerHtml += "html tags " + question + radio buttons containing answers and it keeps selecting from database till all questions are rendered in client side, the thing is i add all these questions and tags in form and in its end there is a input submit button which should submit all radio buttons values but even if i set form action i cant get the radio buttons values...
Here is some code :
using (SqlDataReader sr1 = command1.ExecuteReader())
{
cc.InnerHtml = "<form id=\"form2\" name=\"form2\" method=\"post\" action=\"\">";
while (sr1.Read())
{
i++;
iString = i.ToString();
questionText = sr1["text"].ToString();
question_first_wrong_answer = sr1["first_wrong_answer"].ToString();
question_second_wrong_answer = sr1["second_wrong_answer"].ToString();
question_right_answer = sr1["right_answer"].ToString();
questionValue = (int)sr1["value"];
c = answerPos.Next(0, 10);
cc.InnerHtml += "<span>" + iString + "- " + questionText +"</span>"
+ "<br />"
+ "<table style=\"width:77%; margin-left: 47px;\">"
+ "<tr>"
+ " <td style=\"width: 210px\">"
+ " <input type=\"radio\" name=\"question" + iString + "a\" value=\"" + question_first_wrong_answer + "\" />" + question_first_wrong_answer + "</td>"
+ "<td style=\"width: 210px\">"
+ "<input type=\"radio\" name=\"question" + iString + "a\" value=\"" + question_second_wrong_answer + "\" />" + question_second_wrong_answer + "</td>"
+ "<td style=\"width: 210px\">"
+ "<input type=\"radio\" name=\"question" + iString + "a\" value=\"" + question_right_answer + "\" />" + question_right_answer + "</td>"
+ "</tr>"
+ "</table>"
+ "<br />";
}
sr1.Close();
cc.InnerHtml += "<input id=\"Submit1\" type=\"submit\" value=\"Submit\" />"
+ "</form>";
}

Do not Use InnerHtml. Its Wrong.
Use Inbuild HtmlPage Class
Use Like this for Example
Page.Controls.Add(
new LiteralControl(#"<html>\r\n<body>\r\n
<h1>Welcome to my Homepage!</h1>\r\n"));
HtmlForm Form1 = new HtmlForm();
Form1.ID = "Form1";
Form1.Method = "post";
Form1.Controls.Add(
new LiteralControl("\r\nWhat is your name?\r\n"));
TextBox TextBox1 = new TextBox();
TextBox1.ID = "txtName";
Form1.Controls.Add(TextBox1);
Form1.Controls.Add(
new LiteralControl("\r\n<br />What is your gender?\r\n"));
DropDownList DropDownList1 = new DropDownList();
DropDownList1.ID = "ddlGender";
ListItem ListItem1 = new ListItem();
ListItem1.Selected = true;
ListItem1.Value = "M";
ListItem1.Text = "Male";
DropDownList1.Items.Add(ListItem1);
ListItem ListItem2 = new ListItem();
ListItem2.Value = "F";
ListItem2.Text = "Female";
DropDownList1.Items.Add(ListItem2);
ListItem ListItem3 = new ListItem();
ListItem3.Value = "U";
ListItem3.Text = "Undecided";
DropDownList1.Items.Add(ListItem3);
Form1.Controls.Add(
new LiteralControl("\r\n<br /> \r\n"));
Button Button1 = new Button();
Button1.Text = "Submit!";
Form1.Controls.Add(Button1);
Form1.Controls.Add(
new LiteralControl("\r\n</body>\r\n</html>"));
Controls.Add(Form1);

Remove the server form element (<form runat="server" ). If that is present, this new form is discarded.
Also you are using the same name for different input controls.
name=\"question" + iString + "a\" value=\""
So you need to have unique names for the radio too.

Related

Avoiding PostBack in asp

I want to avoid postback when my button event fired. Due to postback my List gets empty again. Kindly Help me out with this. What should i do to avoid postback. I am using ASP button.
Here is my button method
id = Convert.ToInt32(Request.QueryString["id"].ToString());
venues.Add(id);
foreach (int item1 in venues)
{
if (!hashset.Add(item1))
{
Response.Write("<script>alert('Duplicate Items');</script>");
break;
}
else
{
foreach (var items in venues)
{
con.Open();
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Venues where VenueID = '" + items + "'";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter ds = new SqlDataAdapter(cmd);
ds.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
Name = dr["Name"].ToString();
Address = dr["Address"].ToString();
Picture = dr["Picture"].ToString();
Price = dr["Price"].ToString();
}
con.Close();
if (Request.Cookies["aa"] == null)
{
Response.Cookies["aa"].Value = Name.ToString() + "," + Address.ToString() + "," + Picture.ToString() + "," + Price.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
else
{
Response.Cookies["aa"].Value = Request.Cookies["aa"].Value + "|" + Name.ToString() + "," + Address.ToString() + "," + Picture.ToString() + "," + Price.ToString();
Response.Cookies["aa"].Expires = DateTime.Now.AddDays(1);
}
}
}
}
}

Url.Action to Html.Raw

I have this code om my View. And this is work good.
... onclick="location.href = '#Url.Action("Plan", "Care", new {Area = "HomeC"})'"><img src="~/Content/images/dash/Care.PNG"
How Can I do the same code with Html.Raw?
I tried this
grid.Column(columnName: "Name", header: "", format: #<text>#(item.ListStatus == 1 ? Html.Raw("<button class='btn btn-default btn-plan' title='Care' onclick='location.href = " + "'" + Url.Action("Plan", "Care", new {Area = "HomeC"}) + "''>" +
"<img src='/Content/images/dash/Care.PNG' class='img-plan' data-interlocutorid=" + item.UserId + " " + "data-interlocutorname=" + item.Name + " " + ">" +
"</button>") : Html.Raw(""))
</text>),
With image all good, but link doesn't work.
Change the single-quotes around your Url.Action(...) to double-quotes instead.
That is, change this portion:
onclick='location.href = " + "'" + Url.Action("Plan", "Care", new {Area = "HomeC"}) + "''
to this:
onclick='location.href = " + "\"" + Url.Action("Plan", "Care", new {Area = "HomeC"}) + "\"'
This will change the rendered output from:
onclick='location.href = '/Care/Plan?Area=HomeC''
to:
onclick='location.href = "/Care/Plan?Area=HomeC"'
onclick=" + "location.href=" + "'" + Url.Action("Plan", "Care", new {Area = "HomeC"}) + "'" + ">"
This is work good.

Syntax error (missing operator) in query expression for date in asp.net

the date in excel is of the date time format and the selected date is also date time format but wots the problem
OleDbDataAdapter da1 = new OleDbDataAdapter("SELECT [ProjectId],[ProjectName],[ManagerID],[ManagerName],[AllocationStartDate],[AllocationEndDate],[horizontalshortname] FROM [" + getExcelSheetName + #"] where [horizontalshortname]="+ "'" + TextBox_Horizontal.Text + "'"
+ " AND [Isonsite]=" + "'" + DropDownList_Location.SelectedItem.Text + "'"
+ " AND [AllocationStartDate]>="+Calendar1.SelectedDate
+ " AND [AllocationEndDate]<="+Calendar2.SelectedDate
, conn);
DataSet ds1 = new DataSet();
da1.Fill(ds1);
//if (ds.Tables[0] != null )
if (ds1.Tables[0].Rows.Count > 0)
{
GridView_Utilization_Search.Visible = true;
GridView_Utilization_Search.DataSource = ds1.Tables[0];
GridView_Utilization_Search.DataBind();
}
else
{
GridView_Utilization_Search.Visible = false;
}

Web forms : sending image into email not displaying in outlook?

I'm sending an image through web form the image is displayed well into gmail ,yahoo etc.
But i'm not able to view that image when I send it on Outlook
what would be wrong please help.
Code:
//Variable mg have a html code with 'cid:uniqueId1’ calls image from function SendMail12
void birthday()
{
Coonection con = new Coonection();
sql = "select es.PORTNO,es.SERVERNAME,es.EMAILID,es.PASSWORD,e.efrom,e.SerNo,e.eto,e.ecc,e.emessage,e.eflag,e.EmpID,e.FIRSTNAME,e.LASTNAME,e.DeptName,e.FromDate,e.Todate,e.Reason from EMAILSETTING es,EmailSender e";
SqlDataAdapter da = new SqlDataAdapter(sql, con.GetConnection());
DataSet ds = new DataSet();
da.Fill(ds);
string sub = "Message";
int portnum, n,sn;
string servername1, passwo, account, t, ecc, mg, flage,mg2;
sql = "select count(srno)from EmailSender";
SqlCommand cmd = new SqlCommand(sql, con.GetConnection());
// dr = cmd.ExecuteReader();
n = Convert.ToInt32(cmd.ExecuteScalar());
//while (i)
//{
for (int i = 0; i < n; i++)
{
if (ds.Tables[0].Rows.Count > 0)
{
portnum = Convert.ToInt32(ds.Tables[0].Rows[0]["PORTNO"].ToString());
servername1 = ds.Tables[0].Rows[0]["SERVERNAME"].ToString(); //Table(0).Rows(0)("SmtpServerName").ToString();
account = ds.Tables[0].Rows[0]["EMAILID"].ToString();
passwo = ds.Tables[0].Rows[0]["PASSWORD"].ToString();
t = ds.Tables[0].Rows[i]["eto"].ToString();
ecc = ds.Tables[0].Rows[i]["ecc"].ToString();
mg2 = ds.Tables[0].Rows[i]["emessage"].ToString();
flage = ds.Tables[0].Rows[i]["eflag"].ToString();
sn = Convert.ToInt32(ds.Tables[0].Rows[i]["SerNo"].ToString());
if (flage == "N" && mg2 == "Wish You a Very Happy Returns of the Day.<P> From - Daccess Security Systems Pvt ")
{
mg = " <html> " +
"<body background= 'meet.jpg'>" +
"<head> " +
"<title>Untitled Document</title> " +
"<meta content='text/plain; charset=us-ascii' http-equiv='Content-Type' />" +
"<style type='text/css'> " +
".style1 { " +
"font-family: Arial, Helvetica, sans-serif; " +
"font-weight: bold; " +
"font-size: 18px; " +
"color: #3333CC; " +
"} " +
".style3 { " +
"font-family: 'Times New Roman', Times, serif; " +
"color: #003399; " +
"} " +
".style4 { " +
"font-family: Arial, Helvetica, sans-serif; " +
"font-weight: bold; " +
"font-size: 12px; " +
"} " +
".style11 { " +
"color: #1A588D; " +
"font-family: 'Times New Roman', Times, serif; " +
"font-weight: bold; " +
"} " +
".style15 { " +
"color: #1A588D; " +
"font-weight: bold; " +
"} " +
".style18 {color: #FFFFFF; font-weight: bold; } " +
"body { " +
"margin-top: 1px; " +
"margin-left: 1px; " +
"margin-right: 1px; " +
"margin-bottom: 1px; " +
"} " +
"</style> " +
"<link href='Untitled-1.htm' id='1' title='view'> " +
"<script language='JavaScript' type='text/JavaScript'> " +
"function MM_reloadPage(init) { " +
"if (init==true) with (navigator) {if ((appName=='Netscape')&&(parseInt(appVersion)==4)) { " +
"document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }} " +
"else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload(); " +
"} " +
"MM_reloadPage(true); " +
"</script> " +
"<style type='text/css'> " +
"a:link { " +
"text-decoration: none; " +
"} " +
"a:visited { " +
"text-decoration: none; " +
"} " +
"a:hover { " +
"text-decoration: none; " +
"} " +
"a:active { " +
"text-decoration: none; " +
"} " +
".style20 {font-size: 12px} " +
"</style></head> " +
"<body> " +
"<p class='style1'><img src='cid:uniqueId' width='145' height='111'></p> " +
"<table border= 1 align ='left'>" +
"<tr>" + "<td>Employee ID :" + "</td>" +
"<td>" + ds.Tables[0].Rows[i]["EmpID"].ToString() + "</td>" +
"</tr>" +
"<tr>" + "<td>Employee Name :" + "</td>" +
"<td>" + ds.Tables[0].Rows[i]["FIRSTNAME"].ToString() + " " + ds.Tables[0].Rows[i]["LASTNAME"].ToString() + "</td>" +
"</tr>" +
"<tr>" + "<td>Department :" + "</td>" +
"<td>" + ds.Tables[0].Rows[i]["DeptName"].ToString() + "</td>" +
"</tr>" +
"<tr>" + "<td>Message :" + "</td>" +
"<td>" + ds.Tables[0].Rows[i]["emessage"].ToString() + "</td>" +
"</tr>" +
"<tr>"+
"<td>"+
"<pre align='left' class='style3'><strong><img src='cid:uniqueId1' width='286' height='177'></strong></pre> " +
"<pre align='left' class='style4'> <a name='t'></a> Please do not reply to this email.</pre>" +
"<p align='left' class='style3'> </p> " +
"</td>"+"</tr>"+"</table>"+
"</body> " +
"</html> ";
if (flage == "N" && mg2 == "Wish You a Very Happy Returns of the Day.<P> From - Daccess Security Systems Pvt ")
{
SendMail12(account, passwo, t, sub, mg, ecc, servername1, portnum);
sql = "Update EmailSender set eflag ='Y' where SerNo = '" + sn.ToString() + "'";
SqlCommand cmd4 = new SqlCommand(sql, con.GetConnection());
cmd4.ExecuteNonQuery();
}
}
}
else
{
servername1 = "";
portnum = 0;
account = "";
passwo = "";
MessageBox.Show("Email Setting Is Not Stored");
}
}
}
// Send mail function
public static bool SendMail12(string gMailAccount, string password, string to, string subject, string message, string cc, string server, int port)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(gMailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(gMailAccount);
msg.To.Add(new MailAddress(to));
string[] s;
s = cc.Split(';');
for (int i = 0; i < s.Length; i++)
{
msg.CC.Add(new MailAddress(s[i].ToString()));
}
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = server;
client.Port = port;
client.Timeout = 100000;
client.EnableSsl = true;
client.UseDefaultCredentials = true;
client.Credentials = loginInfo;
string palinBody = "Plain text content, viewable by those clients that don't support html";
AlternateView plainView = AlternateView.CreateAlternateViewFromString(palinBody, null, "text/plain");
string htmlBody = message;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(htmlBody, null, "text/html");
//create the AlternateView for embedded image
AlternateView imageView = new AlternateView("D:\\Aniket Work\\New Folder Update\\EmailSender\\EmailSender\\images\\Daccess-logo.gif", MediaTypeNames.Image.Gif);
imageView.ContentId = "uniqueId";
imageView.TransferEncoding = TransferEncoding.Base64;
AlternateView imageView1 = new AlternateView("D:\\Aniket Work\\New Folder Update\\EmailSender\\EmailSender\\images\\birthday1images.jpg", MediaTypeNames.Image.Jpeg);
imageView1.ContentId = "uniqueId1";
imageView1.TransferEncoding = TransferEncoding.Base64;
//add the views
msg.AlternateViews.Add(plainView);
msg.AlternateViews.Add(htmlView);
msg.AlternateViews.Add(imageView);
msg.AlternateViews.Add(imageView1);
client.Send(msg);
return true;
}
catch (Exception e)
{
return false;
}
}
You should be giving absolute paths to your resources
e.g.
in above code, your body background should be something like
<body background= 'http://yourdomain.com/meet.jpg'>
same thing applies to your image tag or you can have inline content disposition
EDIT
Do you see something like this when you view the mail?

Why Can't I store XML in ASP.NET ListBox Value?

Why does this work:
ListItem item = new ListItem();
string value = lstAvailExtPropsToFilter.SelectedItem.Text +" = "+ txtExtPropToFilter.Text;
string text = lstAvailExtPropsToFilter.SelectedItem.Text + " = " + txtExtPropToFilter.Text;
item.Text = text;
item.Value = value;
lstExtPropsToFilter.Items.Add(item);
But not this:
ListItem item = new ListItem();
string value = string.Format("<key>{0}</key><value>{1}</value>", lstAvailExtPropsToFilter.SelectedItem.Text, txtExtPropToFilter.Text);
string text = lstAvailExtPropsToFilter.SelectedItem.Text + " = " + txtExtPropToFilter.Text;
item.Text = text;
item.Value = value;
lstExtPropsToFilter.Items.Add(item);
You would have to escape the string so it works in HTML. Try Server.HTMLEncode.
Server.HTMLEncode("<key>{0}</key><value>{1}</value>")
Then when you need to pull the value you, use Server.HTMLDecode.

Resources