Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid: Control in EditItem filled by javascript is empty at update

Status
Not open for further replies.

Alcar

Programmer
Sep 10, 2001
595
US
Hi,

In my datagrid I have a checkbox that will put a date in a textbox when clicked on. I have a javascript that will put the date in the textbox during a DataGrid Edit.
When I try to update my datagrid, my textbox is empty.

I have done this many times before but somehow I am missing something.

DataGrid HTML Code
Code:
<asp:datagrid id="DataGrid1" runat="server" BorderStyle="None" OnCancelCommand="doCancel" OnUpdateCommand="doUpdate" OnEditCommand="doEdit" CellSpacing="1" CellPadding="1" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Date Paid" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:TextBox ID="textbox1" Enabled=false Runat="server" ></asp:TextBox>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txbDatePaid" Runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
				
<asp:TemplateColumn HeaderText="Paid" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:CheckBox ID="cboUnpaid" Enabled="false" Runat="server"></asp:CheckBox>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="cboPaid" Runat="server"></asp:CheckBox>
</EditItemTemplate>
</asp:TemplateColumn>

<asp:EditCommandColumn ButtonType="LinkButton" EditText='&lt;img src="images\edit.gif" border=0 alt="Edit Paid Option"&gt;' UpdateText='&lt;img src="images\update.gif" border=0 alt="Update Paid Option"&gt;' CancelText='&lt;img src="images\cancel.gif" border=0 alt="Reset Option"&gt;'></asp:EditCommandColumn>
</Columns>
</asp:datagrid>

Javascript code
Code:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  ListItemType lItem = e.Item.ItemType;			
  CheckBox cboPaid = (CheckBox) e.Item.FindControl("cboPaid");

  if (lItem == ListItemType.EditItem)
  {
    iCurrentRow = e.Item.ItemIndex;
    StringBuilder sb = new StringBuilder();
    sb.Append("<Script Language=Javascript>");
    sb.Append("function putdate() { ");
    sb.Append("if(document.getElementById('DataGrid1__ctl" + (2 + iCurrentRow).ToString() + "_cboPaid').checked == true) {");
    sb.Append("var myDate = new Date(); ");
    sb.Append(" document.getElementById('DataGrid1__ctl" + (2 + iCurrentRow).ToString() + "_txbDatePaid').value ='' + (myDate.getMonth()+1) + '/' + myDate.getDate() + '/' + myDate.getFullYear();} }");
    sb.Append("</Script>");
    RegisterStartupScript("putdate",sb.ToString());
    cboPaid.Attributes.Add("onclick","javascript:return putdate();");				
  }
}

my Update function
Code:
public void doUpdate(Object sender, DataGridCommandEventArgs e)
{
  CheckBox cboPaid; 
  TextBox txbDatePaid;	
  cboPaid = (CheckBox) e.Item.FindControl("cboPaid");
  txbDatePaid = (TextBox) e.Item.FindControl("txbDatePaid");

if(cboPaid.Checked)
{
  // always returns false!!
}
else
{
  // always returns false!!
}

if(txbDatePaid.Text !="")
{
  // textbox is always empty!!
}
else
{
  // textbox is always empty!!
}
}

Why is my checkbox always unchecked and why is my textbox always empty?
Thank you in advance...


Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
Since you are using

cboPaid and txbDatePaid as id's in your .aspx page, don't you need to assign values as such:

Code:
paid boolean;
datepaid string;

paid = CType(e.Item.FindControl("cboPaid"), CheckBox).Checked;

datepaid = CType(e.Item.FindControl("txbDatePaid"), TextBox).Text;

Then do your tests on those variables.

-Tyler

 
There is no such thing as CType in c#.


Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
First of all how do you declare a javascript function like this:
private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
I may be missing something but this is not correct syntax for a javascript function.

Before submitting the form enable your textboxes, I don't understand the code you provided but I had the same problem when setting a disabled textbox with javascript (popup calendar). When submitting the form there was no value (empty .text property) server side. So I added javascript on the form (onsubmit) that enabled (disabled=false) the textbox.



Greetings, Harm Meijer
 
Sorry, didn't see it was j# attaching the javascript.
Anyway you can attach the onsubmit attribute of the form calling the following javascript function:

Code:
function formsubmit(){
var formObjects =YourForm.elements;
var i = 0;
while(i<formObjects.length){
	try{
		formObjects[i].disabled=false;
	}catch(e){}
	i++;
}
}



Greetings, Harm Meijer
 
private void DataGrid1_ItemCreated
is a C# code behind function that is called when every row of the datagrid is created. It is not javascript.

MY Javacript works fine. it puts the date and everything. the problem is that when the server-side code is called, it finds the two controls without changes.

Daren J. Lahey
Just another computer guy...
FAQ183-874 contains &quot;Suggestions for Getting Quick and Appropriate Answers&quot; to your questions.
Support your forums TODAY!
 
> the problem is that when the server-side code is called,
> it finds the two controls without changes

That's because they are disabled.
Run this javascript before you submit:
Code:
function formsubmit(){
var formObjects =YourForm.elements;
var i = 0;
while(i<formObjects.length){
    try{
        formObjects[i].disabled=false;
    }catch(e){}
    i++;
}
}

The controls are enabled when you submit and the form is submitted, serverside code does something and rebuilds the page. When rebuilding the page all controls that need disabaling are disabled:

<asp:TextBox ID="textbox1" Enabled=false Runat="server" ></asp:TextBox>





Greetings, Harm Meijer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top