Hi all,
I have a couple of questions in regards to how to retrieve (get) the checkbox state value.
1. I have a an ultrawebgrid with three columns, col1 = Customer ID, col2 = Customer Name, col3=checkbox. (When the checkbox is clicked, it will determine wether an email will be sent out to this customer or not). The checkbox that I used is the one that comes with the webgrid. So all I did was create my column and set the Type="checkbox"
My webgrid has many rows and so from the webgrid and I would like to click on the check box for some customers (records) and not for some. Then I click the Save button to save (update the database) all of the records that have been checked. So on the code behind, how do I determine the value of the Customer ID, Customer Name and the checkboxes that have been selected on the Save button event? My Save button is a regular asp:Button (asp.net 2.0).
2. Would it be better to use an asp:checkbox and embedded inside the ultrawebgrid TemplateColumn?
Please let me know if option one is better or option two is better. Please provide some code sample if possible.
Thanks in advance!
hs2
Use the following code to get the checked columns:
foreach (UltraGridRow row in Grid.Rows)
{ if (row.Cells[columnIndex].Value.ToString().Equals("True")) { // CheckBox is checked }
else
{
// CheckBox is not checked.
} }
Hello Charuk,
I have tried to use the code you suggested above but it doesn't work. I loaded up my webgrid and click on the the checkbox and on the OnActiveCellChange event method and UpdateRowBatch event method, I tried determining the value of the check box but it returns False for the checked checkbox.
What event method did you try to above code on? This way I can use the same event method and try to see if it works and wether it would give me a True value when the checkbox is checked.
Thanks!
Hi hs2!
You said you want to determine the checked columns on the click of save. So try this code on the click event of the save button. I have also written this code on the click event of the button.
Regards,
Charuk
Hello Charuk.,
i copied and pasted your code into my Save button event method and tried it out, it still gives me the status of False even though the check box is checked. I even hardcoded the column name and columnindex just to test out but still doesn't work. Can you be more specific with the example like include the Save button click event and how you also add the checkbox column in the html side? I am not sure what I miss that it doesn't work.
THanks alot in advance!
HS2
I am setting the property ColumnType=CheckBox property of the UltraGridColum. I am doing everything by coding.... On button click m just takin da instance of the grid and writing dat code and it works fine for me.
How r u checking the checkbox? If u r doing it through code behind, then the checkbox values will not persist... you have to do it through javascript.
Plz give some code so that I can see if somthig is wrong....
Below is the sample code of how I created my checkbox.
<igtbl:UltraGridColumn AllowUpdate="Yes" BaseColumnName="SendEmail" DataType="System.Boolean" EditorControlID="cbSendEmail" Key="SendEmail" NullText="" Type="CheckBox"> <Header Caption="Send Email"> <RowLayoutColumnInfo OriginX="7" /> </Header> <Footer> <RowLayoutColumnInfo OriginX="7" /> </Footer> </igtbl:UltraGridColumn>
protected void BtnSave_Click(object sender, EventArgs e) {
foreach (UltraGridRow row in ultrawebgrid1.Rows) { string sTest = row.Cells[0].Value.ToString(); //string sChecked = row.Cells.FromKey("SendEmail").Value.ToString(); string sChecked = row.Cells[7].Value.ToString().ToLower(); //string sChecked = row.Cells.FromKey("SendEmail").Value.ToString().ToLower();
if (sChecked.Equals("true")) {
} } }
I check the checkbox on the actual web page. I don't check the checkbox in the code behind. In the code behind, I only try to find the checkbox state (which it should return me the state of "true").
Thanks alot in advance!
I think the problem might be with the "BaseColumnName" property. Just try removing this property and try. I am not sure about this but give it a try....
You have set "BaseColumnName" property to SendMail and u must be binding the data on each postback to display the data, right? So what i think is that the binding of checkbox column is overriding the current state of the checkbox. Say u have checked a checkbox, so its current state should be "True", but when u are hitting save button postback is happening and data is binding to it again and must be overriding the current state of the checkbox.
Just remove this property and check....
Charuk.
Hi Colette
I am new to dotnet, could you please mention the code for initialising webgrid in page load(), does that mean setting the webgrid.datasource to null or the code which we write in initialisedatasource event?
ThankYou in advance
Hi,
I am facing the same issue. And I do not even have problem with re-binding grid on each postback.
But the thing is, I have a webgrid in user control and this user control is getting loaded dynamically on aspx page.
If I postback the page after checking / unchecking every checkbox, the state of checkbox is retained. But if there is no postback after every cell update in webgrid, on final "save" click, checkbox state always shows "Checked = false" even though on the screen, checkboxes are seen chekced.
Any guesses, why checkbox looses the stae?
Thanks & Regards,
Rasika.
Hello Colette,
Thanks so much!! It worked! I can't believe that was all I missed!
Take care and happy coding!!
Hi HS2,
In the Page_Load method of your page do you have the code for initialising the webgrid surrounded with an if(!Page.IsPostback) block?
If you don't have this block around the webgrid databinding then the grid will be reset when you click the Save button.
Colette
Hi Charuk,
Yep, I tried that the case you suggested above already. But still doesn't work. It seems like it doesn't know that my checkbox even exist. I have no idea why and that's why it keeps on return false. I wish the Infragistic experts would give me an example of exactly how to use checkbox on the server side. I want to be able to get the checkbox state on the server side so I can update the database so that way it use a service to send out email to customers. I think I might have a post a new question in order to get their attention. I see alot of people having problem with checkboxes. Alot of people are posting the same sort of question as me but getting no answer.
Thanks Charuk for all of your help. I really appreciate that!!! If you see anything or have any suggestion, I would love to if you can forward it to.