I have a grid wih some 25 records.
While inserting a new record need to check whether it constains duplication.
So I have taken the recods in the grid and checked.
protected
void RadGridEmpType_InsertCommand(object source, GridCommandEventArgs e)
{
GridEditFormInsertItem insertedItem = (GridEditFormInsertItem)e.Item;
Hashtable newValues = new Hashtable();
e.Item.OwnerTableView.ExtractValuesFromItem(newValues, insertedItem);
int value = 0;
RadTextBox txt = (RadTextBox)e.Item.FindControl("txtTypeShortDesc");
int Rowcount = RadGridEmpType.Items.Count;
for (int count = 0; Rowcount > count; count++)
{
Label lbl = (Label)RadGridEmpType.Items[count].FindControl("LblTypeShortDesc");
if (txt.Text.ToUpper() == lbl.Text.ToUpper())
{
value++;
}
}
if (value == 0)
{
dsiSmartEmpType.InsertParameters[0].DefaultValue = CompanyID.ToString();
dsiSmartEmpType.InsertParameters[1].DefaultValue = UserId;
}
else
{
Label lbl = (Label)e.Item.FindControl("Msg");
lbl.Text =
"Already Exists";
e.Canceled =
true;
}
}
It works.
But It just takes the record in the current page.
For example the grid contains 25 records. and the page size is given as 10.
While inserting it just checks the last five.
Is there any option to take all the 25 records in the grid.
Thank You.
-Anto
6 Answers, 1 is accepted
I tried setting the AllowPaging property to false and then loop through the griditems for checking whether the values already exists or not. Give a try with this approach.
C#:
protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e) |
{ |
TextBox txt = (TextBox)e.Item.FindControl("txtTypeShortDesc"); |
RadGrid1.AllowPaging = false; |
RadGrid1.Rebind(); |
foreach (GridDataItem item in RadGrid1.MasterTableView.Items) |
{ |
if (txt.Text == item["LblTypeShortDesc"].Text.ToString()) |
{ |
Response.Write("Already exists"); |
e.Canceled = true; |
} |
} |
RadGrid1.AllowPaging = true; |
RadGrid1.Rebind(); |
} |
-Shinu.
I have replaced the existing set of code with the code you have sent.
If I try to insert a record.
An error appears.
Cannot find a cell bound to column name 'LblTypeShortDesc'
In my aspx page. I have used:
<
telerik:GridTemplateColumn UniqueName="TypeShortDesc" DataField="TypeShortDesc" ItemStyle-Width="15%"
HeaderText="Short Description">
<ItemTemplate>
<asp:Label ID="LblTypeShortDesc" Text='<%#Eval("TypeShortDesc")%>' runat="server"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txtTypeShortDesc" TextMode="SingleLine" MaxLength="5" Text='<%#Bind("TypeShortDesc")%>'
runat="server" ToolTip="Maximum Character Allowed is 5" ></telerik:RadTextBox><asp:Label ID = "Label1" runat = "server" ForeColor = "Red" Text="*" ></asp:Label>
<asp:RequiredFieldValidator ID="RequiredFieldValidatorTypeShortDesc" runat="server"
ErrorMessage="" ControlToValidate="txtTypeShortDesc"></asp:RequiredFieldValidator>
<asp:Label ID = "Msg" runat = "server" ForeColor = "Red"></asp:Label>
<asp:RegularExpressionValidator ID="RegularExpressionValidatorTypeShortDesc" runat="server"
ControlToValidate="txtTypeShortDesc" ValidationExpression="^[a-zA-Z0-9]+$" ErrorMessage="Special Characters not Allowed"></asp:RegularExpressionValidator>
</EditItemTemplate>
</telerik:GridTemplateColumn>
-Anto
|
if (txt.Text == ((Label)item.FindControl("LblTypeShortDesc").Text).ToString()) //replaced |
{ |
Response.Write("Already exists"); |
e.Canceled = true; |
} |
I raplaced the line. Then I got the error.
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'Already exists16964|'.
Then I replaced the Lines:
if (txt.Text == (((Label)item.FindControl("LblTypeShortDesc")).Text).ToString()) |
{ |
Response.Write("Already exists"); |
e.Canceled = true; |
} |
To
if (txt.Text == (((Label)item.FindControl("LblTypeShortDesc")).Text).ToString()) |
{ |
Label lbl = (Label)e.Item.FindControl("Msg"); |
lbl.Text = "Already Exists"; |
e.Canceled = true; |
} |
Now No Error appears.
But Nothing happened when the insert button is clicked.
Record was not inserted, But the message didn't appear "Already Exists".
-Anto
mark
I am hoping you can help, I am trying to do somewhat the idea as stated above.
Please see my thread here: http://www.telerik.com/community/forums/aspnet-ajax/grid/how-to-check-if-grid-item-exists-in-second-grid.aspx
Basically I want two test if a value already exist in a second grid before my insert runs from a button click event.
Would appreciate any help you can provide.
Thanks
Brett