Click here to Skip to main content
16,003,902 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi everyone,

I am making a website in Asp.net using C#, i have one gridview and in that gridview i have template fields for edit and deleting records..
Now what happens is when page loads and when i click on that gridview's edit OR delete option but after postback it works..

I have used this code..
<asp:GridView ID="gdvAssetType" runat ="server" AllowPaging="True"
                               AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC"
                               BorderStyle="None" BorderWidth="1px" CellPadding="3"
                               EnableModelValidation="True"
                               onpageindexchanging="gdvAssetType_PageIndexChanging"
                               onrowcommand="gdvAssetType_RowCommand" Width="747px">
                               <Columns>
                                   <asp:TemplateField HeaderText="Sr No.">
                                       <ItemTemplate >
                                           <asp:Label ID="lbSerialNo" runat ="server" Text='<%#Container.DataItemIndex+1 %>'></asp:Label>
                                       </ItemTemplate>
                                       <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                   </asp:TemplateField>
                                   <asp:BoundField HeaderText="Asset Type" DataField="AssetType">
                                   <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                   </asp:BoundField>
                                   <asp:TemplateField HeaderText="Edit">
                                       <ItemTemplate >
                                            <asp:ImageButton ID="imgbtnEdit" runat ="server" CommandArgument ='<%#Eval("AssetTypeId") %>' CommandName ="EditAssetType" ImageUrl ="../_img/ico_edit_new.gif"  CausesValidation ="false" ToolTip ="Edit Asset Type"/>
                                       </ItemTemplate>
                                       <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                   </asp:TemplateField>
                                   <asp:TemplateField HeaderText="Delete">
                                       <ItemTemplate >
                                           <asp:ImageButton ID="imgbtnDelete" runat ="server" CommandArgument ='<%#Eval("AssetTypeId") %>' CommandName ="DeleteAssetType"  ImageUrl ="../_img/ico_del_new_on.gif" CausesValidation ="false" OnClientClick="if (!confirm('Do you want to delete this user ?')) return false;"  ToolTip ="Delete Asset Type"/>
                                       </ItemTemplate>
                                       <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" />
                                   </asp:TemplateField>
                               </Columns>
                               <FooterStyle BackColor="White" ForeColor="#000066" />
                               <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                               <PagerSettings Position="TopAndBottom" />
                               <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                               <RowStyle ForeColor="#000066" />
                               <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                           </asp:GridView>


My page load code...

C#
protected void Page_Load(object sender, EventArgs e)
   {
       try
       {
           if (Session["Username"].ToString() != "" && Session["Username"] != null && Session["Role"].ToString() != "" && Session["Role"] != null && Session.Count != 0)
           {
               if (!IsPostBack)
               {

                   FillGDVAssetTypes();
               }
           }
           else
           {
               Response.Redirect("../Login.aspx");
           }

       }
       catch { Response.Redirect("../ErrorPage/Error.aspx"); }
       finally { }
       //AssignRights();
   }


now on delete template field click it first prompts user that "Do you want to really delete the record" , but it doesn't works on page load ... Both edit and delete record only works after postback...

Please help me out ..

Thanks & Regards,
Krunal Panchal
Posted
Updated 24-Jun-12 18:54pm
v3
Comments
DamithSL 23-Jun-12 11:32am    
Can you update the question with what you have written in page load?
[no name] 25-Jun-12 0:38am    
Hi Damith...

What i have written in page load is ..

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["Username"].ToString() != "" && Session["Username"] != null && Session["Role"].ToString() != "" && Session["Role"] != null && Session.Count != 0)
{
if (!IsPostBack)
{

FillGDVAssetTypes();
}
}
else
{
Response.Redirect("../Login.aspx");
}

}
catch { Response.Redirect("../ErrorPage/Error.aspx"); }
finally { }
//AssignRights();
}

Please help me out....

Regards,
Krunal panchal

1 solution

In your gdvAssetType_RowCommand method after update or delete you can call FillGDVAssetTypes(); method to rebind the data.
 
Share this answer
 
Comments
AshishChaudha 25-Jun-12 1:25am    
my 5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900