Welcome to Dream.In.Code
Become an Expert!

Join 137,211 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,270 people online right now. Registration is fast and FREE... Join Now!




Gridview Index

 
Reply to this topicStart new topic

Gridview Index, How to read Index when I click a Button in a LinkButton in a gridView

H3noy
10 Jul, 2008 - 07:05 PM
Post #1

New D.I.C Head
*

Joined: 10 Jul, 2008
Posts: 1

Hello Expert,
My name Neysha. I'm beginner in ASP.NET
i have a code to show popup like this :
CODE

<asp:GridView AutoGenerateColumns ="False" ID="ViewDetailsAC" runat ="server" CellPadding="3" ForeColor="Black"                 GridLines="Vertical" Width="894px" BackColor="White" BorderColor="#999999"   BorderStyle="Solid" BorderWidth="1px"  >
                    <Columns >
                        <asp:TemplateField HeaderText="No Asset">
                            <ItemTemplate >                                                                
                                <asp:LinkButton ID="LinkAsset" Text ="Input" runat ="server" onclick="btnInput_Click" CommandName ="commandInput"                                CommandArgument ='<%# DataBinder.Eval(Container, "RowIndex") %>'                                ></asp:LinkButton>
  <cc1:ModalPopupExtender ID="LinkAsset_ModalPopupExtender" runat="server" PopupControlID="PanelNoAsset" TargetControlID="LinkAsset" BackgroundCssClass = "modalBackground" >
                                </cc1:ModalPopupExtender>                                
                            </ItemTemplate>
                        </asp:TemplateField>        
                              </Columns>                    
                </asp:GridView>


When i clicked "LinkAsset", i can show a modalpopup and i can input a value of asset in a textbox name "txtAsset", but when i click save, i wanna update table detail in my database, i mean update asset number and status..
in table details i have 3 primary keys,and the problems is i can't read Row Index in gridview when "LinkAsset" was Clicked..Can anyone help me?
User is offlineProfile CardPM
+Quote Post

avolel
RE: Gridview Index
20 Aug, 2008 - 07:11 AM
Post #2

New D.I.C Head
*

Joined: 24 Aug, 2005
Posts: 38



Thanked: 1 times
My Contributions
QUOTE(H3noy @ 10 Jul, 2008 - 08:05 PM) *

Hello Expert,
My name Neysha. I'm beginner in ASP.NET
i have a code to show popup like this :
CODE

<asp:GridView AutoGenerateColumns ="False" ID="ViewDetailsAC" runat ="server" CellPadding="3" ForeColor="Black"                 GridLines="Vertical" Width="894px" BackColor="White" BorderColor="#999999"   BorderStyle="Solid" BorderWidth="1px"  >
                    <Columns >
                        <asp:TemplateField HeaderText="No Asset">
                            <ItemTemplate >                                                                
                                <asp:LinkButton ID="LinkAsset" Text ="Input" runat ="server" onclick="btnInput_Click" CommandName ="commandInput"                                CommandArgument ='<%# DataBinder.Eval(Container, "RowIndex") %>'                                ></asp:LinkButton>
  <cc1:ModalPopupExtender ID="LinkAsset_ModalPopupExtender" runat="server" PopupControlID="PanelNoAsset" TargetControlID="LinkAsset" BackgroundCssClass = "modalBackground" >
                                </cc1:ModalPopupExtender>                                
                            </ItemTemplate>
                        </asp:TemplateField>        
                              </Columns>                    
                </asp:GridView>


When i clicked "LinkAsset", i can show a modalpopup and i can input a value of asset in a textbox name "txtAsset", but when i click save, i wanna update table detail in my database, i mean update asset number and status..
in table details i have 3 primary keys,and the problems is i can't read Row Index in gridview when "LinkAsset" was Clicked..Can anyone help me?


In design view add the onrowdatabound attribute to the grid view markup.
Then in code behind implement GridView1_RowDataBound method.
CODE

<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound">

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
          //In here get reference to the button that was clicked and set its command name to
          // e.Row.RowIndex.ToString();
          Button btn = (Button)e.Row.FindControl("Button1");
           btn.ComandName = e.Row.RowIndex.ToString();
          //Now in your button's event handler you can retreive the data from a row on the Grid.
      }
}

protected void Button1_Click(object sender, EventArgs e)
{
       Button btn = (Button)sender;
       SomeControlthatHoldsIdToUpdateDB x = (SomeControlthatHoldsIdToUpdateDB)GridView1.Rows[Convert.ToInt32(btn.CommandName)].FindControl("ControlName");

}


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/4/08 01:12PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month