How to add checkbox in html table? - c#

I need to add a checkbox in a html table I know the usual method like
<td>
<input type="checkbox" name ="chk1" />
</td>
My current code is
string html="";
html += "<table>";
html += "<tr><th>" + "A" + "</th><th>" + "B" + "</th><th>" + "C" + </th></tr>";
html += "<tr><td>" + "0" + "</td><td>" + "1" + "</td><td>" + "2" + </td></td>+"<input type="checkbox" name ="chk1" /> " +</td> </tr>";
html += "</table>";
but this gives an error

You have missed some quotes
please use this code
string html="";
html += "<table>";
html += "<tr><th>" + "A" + "</th><th>" + "B" + "</th><th>" + "C" + "</th></tr>";
html += "<tr><td>" + "0" + "</td><td>" + "1" + "</td><td>" + "2" + "</td></td>"+"<input type='checkbox' name ='chk1' /> " +"</td> </tr>";
html += "</table>";

It appears that you're missing a quote, also you can get rid of some of the quotes (and rows):
string html="";
html += "<table>";
html += "<tr><th>A</th><th>B</th><th>C</th></tr>";
html += "<tr><td>0</td><td>1</td><td>2</td></td><input type=\"checkbox\" name =\"chk1\" /></td></tr>";
html += "</table>";

No need of concatenations, just put single quotes outside as given in the example below:
string html = '';
html += '<table>';
html += '<tr><th>A</th><th>A</th><th>C</th></tr>';
html += '<tr><td>0</td><td>1</td><td>2</td></td><input type="checkbox" name ="chk1" /></td></tr>';
html += '</table>';

Related

C# ASP.NET Wrap dynamically added CheckBox Control with a dynamically added Label Control

I want the checkbox wrapped in a label so that if they click the Label it will check the CheckBox. I also need the descArea to diplay once the hover over the Label. These must also be dynamic and created in the code behind. I have 2 ways so far, both with their own issue...
This option doesn't allow the JS functions to be called for the descArea to slide down
caPanel.Controls.Add(new LiteralControl(
"<div class='col-md-6'>"+
"<div id='AButton" + t.Rows[i]["ID"].ToString() + "' class='caLabel' onmouseenter='slideDescriptionDown(this.id);' onmouseleave='slideDescriptionDown(this.id);' >" +
"<label>"));
CheckBox aCheckBox = new CheckBox();
aCheckBox.ID = "checkbox" + t.Rows[i]["ID"].ToString();
caPanel.Controls.Add(aCheckBox);
ca.Controls.Add(new LiteralControl(" " + t.Rows[i]["NAME"].ToString() +
"</label>"+
"</div>"+
"<div class='alert alert-info aDescription' id='descArea" + t.Rows[i]["ID"].ToString() + "'>" +
"<span id='desc" + t.Rows[i]["CA_ID"].ToString() + "'>" + t.Rows[i]["Description"].ToString() + "</span>" +
"</div>"+
"</div>"));
This one doesn't allow the CheckBox to be checked when clicking the Label. I inspected the code and the Label is showing up as a span
caPanel.Controls.Add(new LiteralControl("<div class='row caSectionRow'>" +
"<div class='col-md-6'>"));
Label aName = new Label();
aName.ID = "AButton" + t.Rows[i]["ID"].ToString();
aName.Attributes.Add("class", "caLabel");
aName.Attributes.Add("onmouseenter", "slideDescriptionDown(this.id);");
aName.Attributes.Add("onmouseleave", "slideDescriptionDown(this.id);");
aName.Text = " " + t.Rows[i]["NAME"].ToString();
CheckBox aCheckBox = new CheckBox();
aCheckBox.ID = "checkbox" + t.Rows[i]["ID"].ToString();
caPanel.Controls.Add(aCheckBox);
caPanel.Controls.Add(aName);
caPanel.Controls.Add(new LiteralControl(
"<div class='alert alert-info aDescription' id='descArea" + t.Rows[i]["ID"].ToString() + "'>" +
"<span id='desc" + t.Rows[i]["ID"].ToString() + "'>" + t.Rows[i]["Description"].ToString() + "</span>" +
"</div>" +
"</div>"));
I found the error lied within the JS function itself when trying to display the descArea for the first option I proposed. I didn't have the ID for the descArea correct. It was prepended with the MainContent_aContent_ to fulfill UniqueID property all controls must have in ASP.NET.
When is the ctl00 prefix added?
https://msdn.microsoft.com/en-us/library/1d04y8ss(v=vs.100).ASPX

can not find dynamic html select in code behind

i have a class named "row" that has 3 item {property,instance,degree}.
now i want to show my items or rows to user and if he changed the third part of class i edit it. this is my code of generating rows dynamically, numberOfInstance is number of rows.
<div id="hidden" class="hiddendiv">
<%
string matn = "";
for (int i = 0; i < numberOfInstance ; i++)
{
row r= new row();
r = a[i];
matn+= " <div class='CMSdiv'>";
matn += "<input id='Text"+i+"' class='labelhid' type='text'"+"value = '"+r.Property+ "'/>";
matn += "<input id='Text2" + i + "' class='labelhid' type='text'" + "value = '" + r.instance+ "'/>";
matn += " <select id='Select" +i+ "'class='drophid' runat='server'>";
matn += " <option value= '"+ "خیلی زیاد"+"'>خیلی زیاد" + " <option value= '"+"زیاد'"+">زیاد";
matn += " <option value='"+"متوسط'"+">متوسط";
matn += " <option value='" + "کم'" + ">کم";
matn += "</select>";
matn += "</div>";
Response.Write(matn);
matn = "";
}
%>
</div>
now if user clicked on select and choose one option, i get it's value and pour to r.degree in code behind. but whatever i did i couldn't find it in code behind and was null. these are my attempts:
var val = Request["Select" + i];
string selection = Request.Form["Select" + i];
hd = (HiddenField)Page.FindControl("Select" + i);
hd = (HiddenField)Control.FindControl("Select" + i);
You can't generate server side controls by Response.Write (More info here). So html elements that you are writing to output are client side controls not server side and cannot be found using the Page.FindControl method.
Consider using one of the server side controls like Repeater instead.

Loop Button inside the Loop Div

I'm new here, I'm stuck in problem in which i have to loop my button so that i will be able to put it on my looping div. How will I do that? This is what I got so far:
//my loop
for (i = 0; i < MPreply.recommendation.Length; i++) {
html += "<div class='rec'> ";
html += "</br> <div id = 'from1' value = > Depart From:" + " ";
html += emz + " " + "(" + + ")" + "</div>";
html += "<div id = 'date1' > Schedule:" + " " + flytDate;
html += " , " + convertedString;
html += "</div>" + "<div id = 'to1' >To:" + " " + emz2 + " " + "(" + "" + ")";
html += "</div> </br> </br> </br> ";
html += "<div id = 'RecNo'>" + " " + (i + 1) + " )" + " </br> </br> </div> ";
html += "<div id = 'fare1'> $" + em + " </div> ";
html += " </div> <br/> <br/>";
div_rec.InnerHtml = html;
}
html:
<div id = 'modal' runat="server" >
<div id="dialog" runat="server">
</div>
<input id="_menuitem" type="button" value="Click" runat="server" />
</div>
Thanks in advance.
You have at least a few problems:
"<div id = 'fare1'>
and similar sections will generate multiple elements with the same id.
div_rec.InnerHtml = html;
Should be moved outside of your loop so that div_rec is only updated once, when everything is done.
emz + " " + "(" + + ")" + "</div>";
Should (probably) be changed to
emz + " " + "(" + your_variable_here + ")" + "</div>";
In order to (I think) add the creation of a button in your loop, you'll have to piece it together as a string just like your other elements. To get a function handler wired up you'll have to either:
a) Build a dom level-0 handler as a string, or
b) Wait until your html is updated, then query out the newly added buttons and add event handlers in code (using either btn.onclick = function(... or dom level-2 handlers (addEventListener or attachEvent)
The Problem i see here is with the ID field.
You have mentioned same ID name for all the Div's you are generating.
Like,
<div id = 'from1'>
<div id = 'to1'>
ID is a unique identifier to an element. It must always be unique.
You can Use Class instead.
Like,
<div class = 'from1'>
<div class = 'to1'>

WP7 Webbrowser InvokeScript errors

WebBrowser control's invoke script is always giving me errors. This html script is validated from http://validator.w3.org. I wrote the code such that on clicking "button1" webBrowser1 invokes the function "setCredentials". I am not sure why this is giving an error like
"An unknown error has occurred. Error: 80020006."
public TestInvokeScript()
{
InitializeComponent();
LoadHtml();
webBrowser1.IsScriptEnabled = true;
webBrowser1.NavigateToString(_html);
button1.Content = "Set Credentials";
}
private void LoadHtml()
{
_html = "<!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\" lang=\"en\" xml:lang=\"en\">" +
"<head>" + "<meta name=\"generator\" content=\"HTML Tidy for Linux (vers 6 November 2007), see www.w3.org\" />" +
"<script type=\"text/javascript\">" +
"//<![CDATA[" +
" function setCredentials()" +
" {" +
" document.getElementById(\"email\").value = \"test#gmail.com\";" +
" }" +
"//]]>" +
"</script>" +
"<title></title>" +
"</head>" +
"<body>" +
"<form action=\"https://cloudmagic.com/k/login/send\" method=\"post\">" +
"<input id=\"email\" type=\"text\" value=\"\" /> " +
"<input id=\"password\" type=\"password\" />" +
" <button type=\"submit\" id=\"login_send\">Login</button>" +
" </form>" +
"</body>" +
"</html>";
}
private void button1_Click(object sender, RoutedEventArgs e)
{
var obj = webBrowser1.InvokeScript("setCredentials");
}
What is the mistake I am doing.?
A couple of possibilities:
1. Make sure you call it after PageLoaded or NavigateComplete fires.
2. Try this one:
Dispatcher.BeginInvoke(() =>
{
var result = webBrowser.InvokeScript("javascrpitMethod", param1, param2);
});
I was passing the html in string form. Unknowingly, I used a double slash(//)
and that commented the rest of the string since there are no newLine characters. It took me almost a day to figure this out. Remove the double slashes and CData tag.

Event Triggering in asp.net using javascript

I've created two buttons using javascript; yes and no. I need to get the event triggered when the button is clicked.
i.e... onclick=getvalue().
public void SetYesButton(string msg_button_class) {
this.msgbox_Yes_button = "<input type=\"button\" value=\"Yes\" class=\"" + msg_button_class + "\" onClick=\"getvalue()\";>";
}
public void SetNoButton(string msg_button_class) {
this.msgbox_No_button = "<input type=\"button\" value=\"No\" class=\"" + msg_button_class + "\" onClick=\"document.getElementById('pagedimmer').style.visibility = 'hidden'; document.getElementById('msgbox').style.visibility = 'hidden';\">";
}
Didn't understand your question, but a suggestion.
Remove the penultimate semi-colon:
"<input type=\"button\" value=\"Yes\" class=\"" + msg_button_class + "\" onClick=\"getvalue()\">";

Categories

Resources