Wednesday, March 28, 2012

Validating control inside the datagrid

Hi,
i have dategrid contains one coulum called Action, this edititem template contains edit ,delete, (when edit click the following will appear ) update,cancel.
The thing when ever i click on edit the datagrid need to do some validations in edit mode, i have one textbox and dropdown list.
my code as follows
the code inside datagrid
<asp:TemplateColumn HeaderText="Rate ($/MWH)">
<HeaderStyle Width="10px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtRate runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
<asp:RequiredFieldValidator id="vldtrRate" runat="server" Font-Size="Smaller" ControlToValidate="txtRate" Display="Dynamic"
ErrorMessage="*Required"></asp:RequiredFieldValidator>
<asp:RangeValidator id="vldtrDgRate" runat="server" Font-Size="Smaller" ControlToValidate="txtRate"
Display="Dynamic" MinimumValue="1" MaximumValue="1000000000000" Type="Double" ErrorMessage="*Not a valid number"></asp:RangeValidator>
</EditItemTemplate>
</asp:TemplateColumn>
The following function is back end code trying to findout the control(updatelink buttion)
Private Sub dgMailerMain_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMailerMain.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(1)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("LinkButton2"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information will be deleted as well.');")
End Select
End Sub
problem is it is unable to find the link button
please help me to come out of the problem

e.Item.Cells(1) means second datagrid column, are you sure that's the right column?
Try:
myDeleteButton = CType(e.item.FindControl("LinkButton2"), LinkButton)
you shoud find control regardless of cell in which is placed.

Hi,
first off, in the html you provided here i do not see a definition ofthe LinkButton2. According to your code it should be a secondcolumn (index 1, first column has index 0)
Also, you can use FindControl method of the DataGridItem itself insteadof using FindControl method of a specific cell (you might mix up theindex of the cell - starts with 0 for the first column - and you wouldnot have to worry about your code if later you change the order of thecolumns. Not speaking about the fact that you will have to write lesscode)
Hope this helps

please find my actual coding

<asp:datagrid id="dg" runat="server" Font-Size="X-Small" Width="100%" CellPadding="4" BackColor="White"
BorderWidth="1px" BorderStyle="None" BorderColor="#3366CC" OnCancelCommand="dg_Cancel" OnupdateCommand="dg_Update"
OnEditCommand="dg_Edit" OnDeleteCommand="dg_Delete" AutoGenerateColumns="False">
<FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
<ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="CHARGE_NO" ReadOnly="True" HeaderText="Charge No">
<HeaderStyle Width="8px"></HeaderStyle>
</asp:BoundColumn>
<asp:TemplateColumn HeaderText="Charge Description">
<ItemTemplate>
<asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_DESC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlCharge runat="server" DataTextField="CHARGE_DESC" DataValueField="CHARGE_ID" DataSource="<%# GetCharge %>">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Rate ($/MWH)">
<HeaderStyle Width="10px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id=txtRate runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
<asp:CompareValidator runat="server" id="compPrimeNumber"
Operator="DataTypeCheck" Type="Integer"
Display="Dynamic" ControlToValidate="txtRate"
ErrorMessage = "You must enter an integer value." />
<asp:CompareValidator runat="server" id="compPrimeNumberPositive"
Operator="GreaterThan" Type="Integer"
Display="Dynamic" ValueToCompare="0"
ControlToValidate="txtRate"
ErrorMessage = "You must enter a value greater than zero." />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=" Absorb">
<HeaderStyle Width="4px"></HeaderStyle>
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ABSORB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton id="rdbAbsorb" runat="server" Text="Absorb" GroupName="rdbCharges"></asp:RadioButton><BR>
<asp:RadioButton id="rdbPassthrough" runat="server" Text="Pass Through" GroupName="rdbCharges"></asp:RadioButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton id="lkBtnEdit" runat="server" CausesValidation="False" CommandName="Edit">Edit</asp:LinkButton>
<asp:LinkButton id="lkBtnDelete" runat="server" CausesValidation="False" CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton id="lkBtnCancel" runat="server" CausesValidation="False" CommandName="Cancel">Cancel</asp:LinkButton>
<asp:LinkButton id="lkBtnUpdate" runat="server" CausesValidation="False" CommandName="Update">Update</asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

code behind
Private Sub dgMailerMain_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMailerMain.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(4)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")
End Select
End Sub
it is not finding the control
Are you rebinding the datagrid on each postback?
yes, but not for all postbacks, rebinding whenever required like ofter update/delete ...


Dim myTableCell As TableCell
myTableCell = e.Item.Cells(4)
Dim myDeleteButton As LinkButton
myDeleteButton = CType(myTableCell.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")


Instead of all the above code, you would be better off finding the control directly from the Item property of the object e of type DataGridItemEventArgs something like below.
Dim myDelButton = CType(e.Item.FindControl("lkBtnUpdate"), LinkButton)
myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information willbe deleted as well.');")


tried doesn't work.

It is inside edititem template
In your code you are trying to locate controllkBtnUpdatebut it is defined in the EditItemTemplate and EditItem is not the type of the Item you are looking in. Your case statement is
Case ListItemType.Item, ListItemType.AlternatingItem, ListItemType.SelectedItem
and those templates do not contain the definition of the lkBtnUpdatebutton
Cheers

gr8 to see that..,

can u suggest me the solution..?


Srinivas,
Just see the associtivity of the message to your button. You are trying to associate the message, the before delete message to an update button?? That really doesn't make much sense to me. This Attributes.Add should be on the btnDelete as I see. Dont you??
Exactly, solution would be:
myDeleteButton = CType(myTableCell.FindControl("lkBtnDelete"), LinkButton)

Shravan,
myUpdateButton.Attributes.Add("onclick", "return Client_Validate();")

in this confirm is not the function i am going to use, that will be other function that returns true or false, inside that i will do validations
like follows
function Client_Validate()
{
var errormsg ="";
//validating Textbox
var Control=window.document.getElementById(document.all.tabCusotmer_txtQuotationStartDate.id);
if (Control.value=="")
{
errormsg = errormsg + "\n- Start Date Is Required";
document.all.divFromdate.style.visibility = "visible";
}
else
document.all.divFromdate.style.visibility = "hidden";
//Validating Dropdown list

Control=window.document.getElementById(document.all.tabCusotmer_ddlProductType.id);
if (Control.options[Control.options.selectedIndex].value ==0)
{
errormsg = errormsg + "\n- Product Type Is Required";
document.all.divddlProductType.style.visibility = "visible";
}
else
document.all.divddlProductType.style.visibility = "hidden";

//Validating Radiobutton
var Control_y=window.document.getElementById(document.all.tabCusotmer_rdbAdjusted.id);
var Control_n=window.document.getElementById(document.all.tabCusotmer_rdbNonAdjusted.id);
if (Control_y.checked || Control_n.checked )
document.all.divMeterType.style.visibility = "hidden";
else
{
document.all.divMeterType.style.visibility ="visible";
errormsg = errormsg + "\n- *Required";
}

if (errormsg =="")
return true;
else
{

return false;
}

}

I hope u understand wt my intension of doing, all client side validations, here is a situation where i can't use validators,
i have multiple tabs in my form, if i use validator control , i can't post back right?, untill all conditions are satisfied. in all tabs,
in my case i have so many add and next buttons.


Bug thanks for ur help,


This is still problem. I have datagrid code follows

*******************************************************************************************

<asp:datagrid id="dgAddStdFixPricing" runat="server" Width="100%" HorizontalAlign="Left" AutoGenerateColumns="False">
<SelectedItemStyle CssClass="GridSelected"></SelectedItemStyle>
<AlternatingItemStyle CssClass="GridEvenLine"></AlternatingItemStyle>
<ItemStyle CssClass="GridOddLine"></ItemStyle>
<HeaderStyle CssClass="GridHeader"></HeaderStyle>
<Columns>
<asp:TemplateColumn HeaderText="Charge Description">
<ItemTemplate>
<asp:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_DESC") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList id=ddlOtherCharges runat="server" DataSource="<%# GetCharge() %>" DataTextField="CHARGE_DESC" DataValueField="CHARGE_ID">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Rate">
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.CHARGE_RATE") %>'>
</asp:TextBox>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText=" Absorb">
<HeaderStyle Width="4px"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblAbsorb" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.ABSORB") %>'>
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:RadioButton id="rdbAbsorb" runat="server" Text="Absorb" GroupName="rdbCharges"></asp:RadioButton><BR>
<asp:RadioButton id="rdbPassthrough" runat="server" Text="Pass Through" GroupName="rdbCharges"></asp:RadioButton>
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Action">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkEdit" Text="Edit" CommandName="Edit" CausesValidation="True"></asp:LinkButton>
<asp:LinkButton id="lnkBtnDelete" runat="server" CausesValidation="True" CommandName="Delete"></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton runat="server" ID="lnkUpdate" Text="Update" CommandName="Update"></asp:LinkButton>
<asp:LinkButton runat="server" ID="lnkCancel" Text="Cancel" CommandName="Cancel" CausesValidation="false"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" Position="TopAndBottom" CssClass="GridPager" Mode="NumericPages"></PagerStyle>
</asp:datagrid>

********************************************************************************************
Private Sub dgAddStdFixPricing_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgAddStdFixPricing.ItemCreated
Dim myTableCell As TableCell
myTableCell = e.Item.Cells(1)
Dim myDeleteButton As LinkButton
Response.Write(e.Item.Cells(3).HasControls) 'Returs flase
'myDeleteButton = CType(myTableCell.FindControl("lnkBtnDelete"), LinkButton)
' myDeleteButton.Attributes.Add("onclick", "return confirm('Are you sure you want to delete? All relevant information will be deleted as well.');")
End Sub
Can any one help me, where i am wrong.

No comments:

Post a Comment