HI All,
I need to be able to assign a value to my checkbox, so that when the user checks it, the c# code will know what value has been checked. In a plain html tags we can use value attribute to assign a value i.e., <input type="checkbox" id="chk1" value="1">. But <asp:checkbox doesn't have a value field, and i found out that i can use <asp:checkboxlist> & <listItem>.
The actual value is coming from the database like this
When I run the page i get databounding expressions are only supported on objects that have a databinding event....... Is there any way around to this problem.
I understand the point. But I need to pickup a value if this check box is clicked, true/false won't be enough. The checkbox is inside a <asp:repeater> so the value of the checkbox changes according to the row in question. The idea is the user checks the box and click a button and the event handler method will do the needful according to the value provided by the checkbox.
Hope i make sense. Any more suggestion?
THanks
Do you mind doing that through code? Then it is easy enough to set the value of the text property of asp:checkbox inside the repeater. Suppose your html code is something like
Then you can use the code given below in your code behind.
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("your connection string");
string sql = "select field1 from table1";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "MyTable");
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
Label1.Text = "";
}
protected void mybutton_Click(object sender, EventArgs e)
{
for (int i = 0; i < Repeater1.Controls.Count; i++)
{
try
{
CheckBox chk = (CheckBox)Repeater1.Controls[i].FindControl("mycheckbox");
if (chk.Checked)
Label1.Text += chk.Text + " is checked, ";
else
Label1.Text += chk.Text + " is not checked, ";
}
catch
{
}
}
}
I have assumed a field name of field1 and a table name of table1. Using this code in a default page will put the text values of the check box in each row of repeater1 equal to the value of field1. Then if you check or uncheck the check boxes and click on the button, in the mybutton_Click procedure you can find out which check box is clicked as shown in the code. This is printed in Label1.
Thanks for your reply.
My column needs to have a checkbox and a hyperlink that users can click. So i can't have a text property for the checkbox. Bascially it should be a checkbox with just a value attached to it.
Here you can have a html check box with a value set inside a repeeater
And your code behind would have
Code:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("your connection string");
string sql = "select field1 from table1";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "MyTable");
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
Label1.Text = "";
}
protected void mybutton_Click(object sender, EventArgs e)
{
for (int i = 0; i < Repeater1.Controls.Count; i++)
{
try
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Controls[i].FindControl("mycheckbox");
if (chk.Checked)
Label1.Text += "Checkbox with value " + chk.Value + " is checked, ";
else
Label1.Text += "Checkbox with value " + chk.Value + " is not checked, ";
}
catch
{
}
}
}
If you put this code in a default page and place the proper connection string, fields and tables you will get your repeater with check boxes that have a value associated with them but no text. On clicking the button a label will display the details of check boxes and their values and also tell you whether they were checked or not. I have assumed a field name of field1 and a table name of table1 which can be easily replaced.
The point is even though they are html check boxes they still have the runat="server" tag attached to them and are essentially server controls. If you need a check box that is fully a html control and no runat="server" tag attached there are still ways to do that.
If what I have given now does not satisfy your need let me know. I am not clear about the link part of your statement though.
I always get the if condition false even if the checkbox is checked. I tried with the simple <asp:checkbox> to see if after clicking the button, the onclick code knows the checkbox is clicked, no, it's still the same. But if the page loads with checkbox checked, then the onclick button knows it. Am i missing something here?
Thanks for you time.
I had Ispostback function at the wrong place in the page-load....it is working now.
But i can't make the popup window appear at the first click of the button. It works only the second time. Also, if i have the arguments in the url(in window.open) it doesn't work. Any suggestion?
Last edited by tarunk; March 12th, 2009 at 08:25 AM.
Reason: adding
HOw manyever times you click the button, it goes to this code and does nothing. But hardcode the url in the window.open, it opens the file at the second click of the button.
I clicked on view source of the page after clicking the button, it displays as
I think we are talking at cross purposes. I will state your requirement as I understand it and give you a sample application. If it is not correct let me know what exactly you need so that I can help you further. As I understand it you want to have an input check box and an input button in each row of a repeater. The check box should have a value associated with it which comes from a field in a table in your database.
Now on clicking the button on any row of the repeater you want to open a page called targetpage.aspx and pass the value associated with the input check box on that row to this page, if the check box on that row is checked. IF the check box is not checked then nothing should happen.
This can be done using a combination of javascript and c#. I will give you a sample application which you can try out. First open up a default web site in VS2005. It will have a page called Default.aspx in it. Substitute the html code given below for Default.aspx
And here is the code for Default.aspx.cs. You can simply substitute the code again.
Code:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("Your connection string here");
string sql = "select field1 from table1";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "MyTable");
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
Label1.Text = "";
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlInputCheckBox mychk = (HtmlInputCheckBox)e.Item.FindControl("mycheckbox");
HtmlInputButton btn = (HtmlInputButton)e.Item.FindControl("mybutton");
string strvalue = mychk.Value;
btn.Attributes.Add("onclick", "return openfunc('" + mychk.ClientID +"');");
}
}
}
Now in the above code you will have to put your appropriate connection string. The database should have a table called table1 in which there is a field called field1. The value of field1 is what will be associated with the check box. You can use the appropriate connection string, field and table names that suit your purpose. In that case you have to change the field1 to the appropriate field name in the html code (Default.aspx) also.
Now you have to add a second page called targetpage.aspx. Simply go to the solution explorer and add a new web form called targetpage.aspx. I am giving the html code and the code behind for this page below. Simply substitute it.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class targetpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["filename"];
}
}
Now if you run this application keeping Default.aspx as the start up page then you will see rows of check boxes and buttons. The check box in each row has a value associated with it which is the value of field1 in each corresponding row of table1 your database. It will be different for you if you have substituted different field and table names. If you check any check box and click on the button a pop up window will open up and it will show targetpage.aspx in it. This window will display a label in which the value of the query string parameter "filename" that you passed will be displayed.
I think it would be clear now. If your requirement is not what I have outlined let me know.
You got my requirement right except one bit where the submit button appears only once after the <asp:repeater>.
The difference I noticed in your code is you have added('"+somevariable+"' at return openfunc....I did that with my urlstring and it seems to recognise the url. But i still have issues with not opening the page at the first click of the button. Also, if none of the checkboxes are checked, i click on the button, it seems to remember the previous checkbox. I'll follow the javascript you suggested.
Last edited by tarunk; March 13th, 2009 at 05:21 AM.
Reason: a
I have changed my code to include the javascript, infact i tried with your example.
Instead of sending the id of the submit button, i am sending id of the check to the javascript.
That makes life even simpler. I give below a sample program again. Once again open up a new default web site in VS 2005. It will have a web page in it called Default.aspx. I give below the code for Default.aspx and Default.aspx.cs. Simply substitute it.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection con = new SqlConnection("Your connection string here");
string sql = "select field1 from table1";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.Fill(ds, "MyTable");
Repeater1.DataSource = ds;
Repeater1.DataBind();
}
}
protected void mybutton_Click(object sender, EventArgs e)
{
string msg = "";
var filename = "filename";
for (int i = 0, j = 0; i < Repeater1.Controls.Count; i++)
{
try
{
HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Controls[i].FindControl("mycheckbox");
if (chk.Checked)
{
msg += filename + j.ToString() + "=" + chk.Value + "&";
j++;
}
}
catch
{
}
}
if (msg != "")
{
int pos = msg.LastIndexOf('&');
msg = msg.Substring(0, msg.Length - 1);
string url = "targetpage.aspx?" + msg;
string openpopup = "<script language=\"javascript\">window.open('" + url + "', 'target', 'Height=400,width=400');</script>";
Response.Write(openpopup);
}
}
}
After substituting the code for Default.aspx and Default.aspx.cs go to solution explorer and add a new page called targetpage.aspx. And here is the code for targetpage.aspx and targetpage.aspx.cs. You can once again substitute it.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class targetpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["filename0"] + " " + Request.QueryString["filename1"];
}
}
I have used the database table name of table1 and field name of field1. One thing you will have to do is to again put in your appropriate connection string and field and table names in Default.aspx and Default.aspx.cs. After doing that you set Defaut.aspx as the start up page and run the application. You will see a row of check boxes and a button at the bottom.
Now if you check any one check boxes and click on the button, targetpage.aspx will open up in a pop up window and it will show the value associated with the check box you had checked. If you check two check boxes and click on the button the pop up window will list the values associated with the 2 check boxes that were checked. But I haven't coded for listing more than values of 2 checked check boxes in targetpage. But you can always do that.
You see what is happening is, say there are 10 check boxes displayed in 10 rows in the repeater and you check any 5 of them and click on the button. Then the values associated with the 5 checked check boxes are being passed to targetpage.aspx as query string parameters filename0, filename1, filename2, filename3 and filename4.
The number of parameters passed depends on the number of check boxes that you have checked before clicking on the button. I have only unraveled a up to 2 values in targetpage. You can always handle that end of it.
Hope this helps.
Warm Regards.
Jay
ps - Tarun, on second thoughts I thought I might as well attach the sample application. Find attached to this post a file called WebSite3.zip. You can simply unzip it and you will get a directory WebSite3. This is the sample web application I developed. It is developed in Visual web developer 2008. But I am sure it will open in VS 2005 also because there is absolutely no code change. You can use this for your purposes after putting in the appropriate connection string, field names and table names in Default.aspx and Default.aspx.cs. If it does not open in VS 2005 then you may have to cut and paste the code snippets I have given above in a VS 2005 web site.
Last edited by sr_jay; March 14th, 2009 at 11:00 PM.
Reason: I created an attachment
The latest code seems to do the trick. Response.Write is the way to go rather than btn.attribute.add(onclick.....). is there any reason why it works only at the second click?
I appreciate your time for writing the code.
I have similar issue, where after opening the popup page, the user gets to rename the filename and on the close of the popup window, the parent window needs to refresh.
but the popup window just goes blank and does nothing. Obviously i am saying to close it and reload it at the same time.
Do you have any suggestion for this situation.
That again is extremely simple. I have attached a sample application again. You can easily download it. The attached application is WebSite80.zip and if you unzip it you will fine a folder called WebSite80. This is the new application. In the target page I have added a button. I will just give the code for the target page here as that is the only thing that changes. The Default page remains the same.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class targetpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Request.QueryString["filename0"] + " " + Request.QueryString["filename1"];
}
protected void Button1_Click(object sender, EventArgs e)
{
// Put the code for whatever update operations you want to do here
Response.Write("<script language='javascript'>window.opener.location=window.opener.location;window.close()</script>");
}
}
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.