1. my .aspx code is
asp:GridView ID="gvaccmaster" DataKeyNames="id"
runat="server" AutoGenerateColumns="False"
HeaderStyle-Font-Bold="true"
OnRowCancelingEdit="gridView_RowCancelingEdit"
OnRowDeleting="gridView_RowDeleting"
OnRowEditing="gridView_RowEditing"
OnRowUpdating="gridView_RowUpdating"
OnRowDataBound="gridView_RowDataBound" Font-Names="Arial"
Font-Strikeout="False" Font-Underline="False"
OnPageIndexChanging="OnPageIndexChanging" CellPadding="2"
Style="text-align: center; vertical-align: middle"
BorderColor="Black" BorderWidth="2px"> <AlternatingRowStyle
BorderStyle="None" /> <Columns> <asp:TemplateField HeaderText="ID"
ConvertEmptyStringToNull="False"> <ItemTemplate> <asp:Label ID="ID"
runat="server" Text='<%#Eval("id") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:Label ID="id" runat="server"
Text='<%#Eval("id") %>' /> </EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Name"
ConvertEmptyStringToNull="False"> <ItemTemplate> <asp:Label
ID="name" runat="server" Text='<%#Eval("name") %>' />
</ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtname"
runat="server" Text='<%#Eval("name") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfv0"
ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Opening
Balance"> <ItemTemplate> <asp:Label ID="opbalance" runat="server"
Text='<%#Eval("openingbalance") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtopbalance" runat="server"
Text='<%#Eval("openingbalance") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv1" ControlToValidate="txtopbalance"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
<asp:RegularExpressionValidator ID="rev4" runat="server"
ErrorMessage="invalid opening balance" ValidationGroup="validaiton"
ControlToValidate="txtopbalance" ForeColor="Red"
ValidationExpression="^\d+$"> </asp:RegularExpressionValidator>
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="Contact Person"> <ItemTemplate> <asp:Label ID="cperson"
runat="server" Text='<%#Eval("contactperson") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtcperson" runat="server"
Text='<%#Eval("contactperson") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv2" ControlToValidate="txtcperson"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="Address"> <ItemTemplate> <asp:Label ID="address"
runat="server" Text='<%#Eval("address") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:TextBox ID="txtaddress" runat="server"
Text='<%#Eval("address") %>' /> <asp:RequiredFieldValidator
runat="server" ID="rfv3" ControlToValidate="txtaddress"
ErrorMessage="*" ForeColor="Red" ValidationGroup="validaiton" />
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="City"> <ItemTemplate> <asp:Label ID="city"
runat="server" Text='<%#Eval("city") %>' /> </ItemTemplate>enter
code here <EditItemTemplate> <asp:DropDownList ID="ddlcity"
runat="server" Height="25px" Width="100px">
<asp:ListItem>Ahmedabad</asp:ListItem>
<asp:ListItem>Baroda</asp:ListItem>
<asp:ListItem>Rajkot</asp:ListItem>
<asp:ListItem>Udaipur</asp:ListItem>
<asp:ListItem>Jaipur</asp:ListItem>
<asp:ListItem>Ajmer</asp:ListItem>
<asp:ListItem>Jodhpur</asp:ListItem>
<asp:ListItem>Bhopal</asp:ListItem>
<asp:ListItem>Indore</asp:ListItem>
<asp:ListItem>Gwalior</asp:ListItem>
<asp:ListItem>Ujjain</asp:ListItem>
<asp:ListItem>Mumbai</asp:ListItem>
<asp:ListItem>Pune</asp:ListItem>
<asp:ListItem>Nagpur</asp:ListItem>
<asp:ListItem>Nasik</asp:ListItem>
<asp:ListItem>Aurangabad</asp:ListItem>
<asp:ListItem>Akola</asp:ListItem> </asp:DropDownList>
</EditItemTemplate> </asp:TemplateField> <asp:TemplateField
HeaderText="State"> <ItemTemplate> <asp:Label ID="state"
runat="server" Text='<%#Eval("state") %>' /> </ItemTemplate>
<EditItemTemplate> <asp:DropDownList ID="ddlstate" runat="server"
Height="25px" Width="150px"> <asp:ListItem>Gujarat</asp:ListItem>
<asp:ListItem>Rajasthan</asp:ListItem> <asp:ListItem>Madhya
Pradesh</asp:ListItem> <asp:ListItem>Maharashtra</asp:ListItem>
</asp:DropDownList> </EditItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Area"> <ItemTemplate> <asp:Label
ID="area" runat="server" Text='<%#Eval("area") %>' />
</ItemTemplate> <EditItemTemplate> <asp:TextBox ID="txtarea"
runat="server" Text='<%#Eval("area") %>' />
<asp:RequiredFieldValidator runat="server" ID="rfv4"
ControlToValidate="txtarea" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Pin Code">
<ItemTemplate> <asp:Label ID="pincode" runat="server"
Text='<%#Eval("pincode") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtpincode" runat="server" Text='<%#Eval("pincode")
%>' /> <asp:RequiredFieldValidator runat="server" ID="rfv5"
ControlToValidate="txtpincode" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev3" runat="server" ErrorMessage="invalid pincode"
ValidationGroup="validaiton" ControlToValidate="txtpincode"
ForeColor="Red" ValidationExpression="\d{6}">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Mobile">
<ItemTemplate> <asp:Label ID="mobile" runat="server"
Text='<%#Eval("mobile") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtmobile" runat="server" Text='<%#Eval("mobile")
%>' /> <asp:RequiredFieldValidator runat="server" ID="rfv6"
ControlToValidate="txtmobile" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev2" runat="server" ErrorMessage="invalid mobile"
ValidationGroup="validaiton" ControlToValidate="txtmobile"
ForeColor="Red" ValidationExpression="^[7-9][0-9]{9}$">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="E-Mail">
<ItemTemplate> <asp:Label ID="email" runat="server"
Text='<%#Eval("email") %>' /> </ItemTemplate> <EditItemTemplate>
<asp:TextBox ID="txtemail" runat="server" Text='<%#Eval("email") %>'
/> <asp:RequiredFieldValidator runat="server" ID="rfv7"
ControlToValidate="txtemail" ErrorMessage="*" ForeColor="Red"
ValidationGroup="validaiton" /> <asp:RegularExpressionValidator
ID="rev1" runat="server" ErrorMessage="invalid email"
ValidationGroup="validaiton" ControlToValidate="txtemail"
ForeColor="Red"
ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*">
</asp:RegularExpressionValidator> </EditItemTemplate>
</asp:TemplateField> <asp:TemplateField HeaderText="Actions">
<ItemTemplate> <asp:Button ID="ButtonEdit" runat="server"
CommandName="Edit" Text="Edit" Width="50px" /> <asp:Button
ID="ButtonDelete" runat="server" CommandName="Delete" Text="Delete"
Width="50px" /> </ItemTemplate> <EditItemTemplate> <asp:Button
ID="ButtonUpdate" runat="server" CommandName="Update" Text="Update"
ValidationGroup="validaiton" Width="60px" /> <asp:Button
ID="ButtonCancel" runat="server" CommandName="Cancel" Text="Cancel"
Width="60px" /> </EditItemTemplate> </asp:TemplateField> </Columns>
<EditRowStyle BorderStyle="Solid" BorderWidth="2px" /> <HeaderStyle
BorderColor="Black" BorderStyle="Solid" BorderWidth="2px"
Font-Names="Cambria" Font-Size="Large" BackColor="#CCCCCC"
Font-Bold="False" ForeColor="Blue" HorizontalAlign="Center"
VerticalAlign="Middle"></HeaderStyle> <RowStyle
HorizontalAlign="Center" VerticalAlign="Middle" /> </asp:GridView>
页面后面的代码是
protected void btnview_Click(object sender, EventArgs e)
{
string query = txtsearch.Text;
foreach(GridViewRow row in gvaccmaster.Rows)
{
row.Visible = false;
}
if(query.Trim() != "")
{
foreach (GridViewRow row in gvaccmaster.Rows)
{
foreach (TableCell cell in row.Cells)
{
string cellText = cell.Text;
if (cell.Text == "" && cell.Controls.Count > 0)
{
cellText = ((LiteralControl)cell.Controls[0]).Text;
}
if (cellText.IndexOf(query) > -1)
{
row.Visible = true; break;
}
}
}
}
else
{
foreach (GridViewRow row in gvaccmaster.Rows)
{
row.Visible = true;
}
}
}
// im getting error on line
//if (cell.Text == "" && cell.Controls.Count > 0)
//{
// cellText = ((DataBoundLiteralControl)cell.Controls[0]).Text;
//}