Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to use RowEditing event of gridview to get datakey value and assign it to hidden field.
But, i failed to get that.

code as :

C#
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            gvUsers.DataSource = Model.dsAllUsers();
            gvUsers.DataBind();
        }
    }


    protected void gvUsers_DataBound(object sender, EventArgs e)
    {
        gvUsers.HeaderStyle.BackColor = System.Drawing.Color.Gray;
    }


    protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        hfVal.Value = gvUsers.DataKeys[e.NewEditIndex].Value.ToString();
        //pnlSave.Visible = false;
    }

</script>



C#
<body  önload="PageLoadEvent();">
<form id="frm" runat="server">
    <div>
               
      <asp:GridView ID="gvUsers" runat="server" ondatabound="gvUsers_DataBound" 
                onrowediting="gvUsers_RowEditing" DataKeyNames="id" 
                onrowcommand="gvUsers_RowCommand">
          <Columns>
              <asp:CommandField ShowEditButton="True" />
          </Columns>
    </asp:GridView>
</div>
</form>
</body>
</html>


Help me. 
Posted

As far as I know.. You are writing the events in Java Script and There is a logic of Java script that it got fired when page loads... So that's y its not getting fired on row editing of the grid view.

Define the events on .cs page and than it gona be fired when you will edit the row.


Hope It will help... or will at least give you a hint to do some thing better.


:)
 
Share this answer
 
Comments
Sant Osha 22-May-13 0:34am    
dude.. its MVC.... :)
bbirajdar 22-May-13 0:57am    
MVC is wrongly tagged. You cannot use Grid or events in MVC
Sant Osha 22-May-13 3:03am    
I have using gridview in mvc and showing data. only problem is that events not working.. if you have answer then plz share.
Hi...

try this one,may helpful to u.

C#
protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
 {
     gvUsers.EditIndex = e.NewEditIndex;
     getdata(); //got data from table(ie:select * from table)
 }


thank u.
 
Share this answer
 
try modifying the code like this....


C#
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {           
            BindData(); //Initial Binding on load
        }
    }
 

    protected void gvUsers_DataBound(object sender, EventArgs e)
    {
        gvUsers.HeaderStyle.BackColor = System.Drawing.Color.Gray;
    }
 

   //Get the data and bind it to the GridView
   private void BindData()
   {
        gvUsers.DataSource = Model.dsAllUsers();
        gvUsers.DataBind();
   }

   protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
   {
        gvUsers.EditIndex = e.NewEditIndex;
        BindData();
   }

 
</script>


- HTH
Thanks,
Nilesh
 
Share this answer
 

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