Solved

gridview edit button

Posted on 2012-03-10
12
672 Views
Last Modified: 2012-03-27
Hi,

I have a gridview that where autogenetrate edit button is set to false.
But I have a link button like so

<asp:Linkbutton id="lnkEdit" runat="server" cssclass="edit" comamndName="edit">Edit</asp:Linkbutton>

So this of course goes into edit mode.

This works fine in IE but not firefox, I have checked that Enable Javascript is enabled as well please help

Thanks,

R8VI
0
Comment
Question by:R8VI
  • 7
  • 5
12 Comments
 
LVL 7

Expert Comment

by:markmiddlemist
Comment Utility
Are you seeing anything come up in the error console, either on loading the page, or on clicking the link?
0
 

Author Comment

by:R8VI
Comment Utility
Hi,

Nope I am getting nothing in the Error console at all

ill put my code here to help

aspx page

<asp:GridView ID="grdAgentSpecficProperty" runat="server" AutoGenerateColumns="False"
                OnRowDataBound="RowDataBound" AllowPaging="True" PageSize="5" OnPageIndexChanging="OnPageIndexChanged"
                CssClass="GridMyPropertySale" OnRowEditing="RowEditing" AutoGenerateEditButton="false"
                OnRowCancelingEdit="RowCancelEdit" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Right"
                PagerSettings-NextPageText=">" PagerSettings-PreviousPageText="<" PagerStyle-CssClass="PageingStyle"
                GridLines="None" OnRowUpdating="RowUpdating">
                <Columns>
                    <asp:TemplateField>
 <ItemTemplate>
                            <table cellpadding="5px" class="AgentPropertiesSaleorLet">
                                <tr>
                                    <td rowspan="8" valign="top">
                                        <asp:Image ID="ImageName" runat="server" alt="Property Images" Height="100px" Width="100px"
                                            CssClass="MainImageSale" src='<%#"/Account/MyFiles/"+ Eval("ImageName") %>' />
                                        <asp:Label ID="lblPropertyID" Visible="false" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"PropertyID") %>'></asp:Label>
                                        <br />
                                        <br />
                                        <asp:LinkButton ID="lnkEdit" runat="server" CssClass="EditButton" CommandName="Edit">Edit</asp:LinkButton>
                                    </td>
                                    <td>
                                        <asp:MaskedEditExtender ID="MaskedEditExtender1" TargetControlID="txtPrice" Mask="9, 999, 999"
                                            MaskType="Number" DisplayMoney="Left" runat="server">
                                        </asp:MaskedEditExtender>
                                        <div class="PoundSymbolforPrice">
                                            £
                                            <asp:TextBox ID="txtPrice" runat="server" Width="100px" Text='<%#Eval("Price") %>'
                                                CssClass="PriceStyle"></asp:TextBox>
                                            <asp:Label ID="lblTypeOfPrice" runat="server" CssClass="TypeofPrice" Text='<%#DataBinder.Eval(Container.DataItem,"TypeOfPrice") %>'></asp:Label>
                                        </div>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblPropertyNumber" CssClass="labelsforMyPropertiesAddress" runat="server"
                                            Text='<%#DataBinder.Eval(Container.DataItem,"PropertyNumber") %>'></asp:Label>
                                        <asp:Label ID="lblStreet" CssClass="labelsforMyPropertiesAddress" runat="server"
                                            Text='<%#DataBinder.Eval(Container.DataItem,"Street") %>'></asp:Label>
                                        <asp:Label ID="lblCity" CssClass="labelsforMyPropertiesAddress" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"City") %>'></asp:Label>
                                        <asp:Label ID="lblCounty" CssClass="labelsforMyPropertiesAddress" runat="server"
                                            Text='<%#DataBinder.Eval(Container.DataItem,"County") %>'></asp:Label>
                                        <asp:Label ID="lblPostCode" CssClass="labelsforMyPropertiesAddress" runat="server"
                                            Text='<%#DataBinder.Eval(Container.DataItem,"PostCode") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblInfoSaleOrLet" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="Property for:"></asp:Label>
                                        <asp:Label ID="lblSaleOrLet" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"SaleOrLet") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblInfoPropertyType" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="Property Type:"></asp:Label>
                                        <asp:Label ID="lblPropertyType" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"PropertyType") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblBedrooms" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bedrooms") %>'></asp:Label>
                                        <asp:Label ID="lblBedroomsInfo" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="bed"></asp:Label>
                                        |
                                        <asp:Label ID="lblReceptions" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Receptions") %>'></asp:Label>
                                        <asp:Label ID="lblNewReceptions" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="Receptions"></asp:Label>
                                        |
                                        <asp:Label ID="lblBathrooms" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Bathrooms") %>'></asp:Label>
                                        <asp:Label ID="lblBathroomsNew" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="Bathrooms"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblServiceChargeorGR" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text='<%#DataBinder.Eval(Container.DataItem,"ServiceChargeorGR") %>'></asp:Label>
                                        <asp:Label ID="lbServiceCargeamount" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"ServiceChargeorGRAmount") %>'></asp:Label>
                                        |
                                        <asp:Label ID="lblTenure" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Tenure") %>'></asp:Label>
                                        |
                                        <asp:Label ID="lblCommissionInfo" runat="server" CssClass="PropertyInfoLabelsMyProperty"
                                            Text="Commission:"></asp:Label>
                                        <asp:Label ID="lblCommission" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Commission") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:Label ID="lblLease" CssClass="PropertyInfoLabelsMyProperty" runat="server" Text="Years Lease:"></asp:Label>
                                        <asp:Label ID="lblLease2" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"NumberOfYearsLease") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <asp:TextBox ID="txtDescription" runat="server" Text='<%#DataBinder.Eval(Container.DataItem,"Description") %>'
                                            Width="90%" CssClass="txtDescriptionSaleOrLet" Height="80px" TextMode="MultiLine"
                                            MaxLength="250" Style="overflow-y: hidden;"></asp:TextBox>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
          </asp:TemplateField>
                </Columns>
                <PagerSettings NextPageText="&gt;" Position="TopAndBottom" PreviousPageText="&lt;">
                </PagerSettings>
                <PagerStyle HorizontalAlign="Right" CssClass="PageingStyle"></PagerStyle>
            </asp:GridView>

Open in new window


c sharp i have only put rowedit code as this is the one that is not working

    protected void RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdAgentSpecficProperty.EditIndex = e.NewEditIndex;
            GetPropertiesInfo();

            Label lblSelectedPropertyID = (Label)grdAgentSpecficProperty.Rows[grdAgentSpecficProperty.EditIndex].FindControl("lblPropertyNewID");
            sPropertyID = lblSelectedPropertyID.Text;
            GetImages();
            GetAllImagesWithoutMainImage();
        }

Open in new window


thanks for our help so far

Thanks,

r8vi
0
 
LVL 7

Expert Comment

by:markmiddlemist
Comment Utility
I'm doing a bit of research as this one has got me intruiged.

To narrow it down in the interim, if you add a RowCommand handler and change the commandname to somthing nonstandard does that fail in firefox well?
0
 

Author Comment

by:R8VI
Comment Utility
Hi,

I tried that and changed it to command name as test and it worked fine,

That could be the soultion but how do i then put the grid view in edit mode

Thanks,

R8VI
0
 
LVL 7

Expert Comment

by:markmiddlemist
Comment Utility
As I understand it you would need to set the EditIndex value on the GridView  to the index of the row in question (or -1 for no row).

The problem you have then is that the row index is not directly accessible in the RowCommand, so I would suggest that in the rowdatabound handler you assign the index to the CommandArgument of the LinkButton, then use this to set the editindex in the command handler.

I know its a bit hacky but it should (theoretically) work
0
 

Author Comment

by:R8VI
Comment Utility
Hi,

Do you have a code sample please ? Or a link that I can see what you mean ?

Thanks,

R8vi
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 7

Accepted Solution

by:
markmiddlemist earned 500 total points
Comment Utility
ok, using the pattern I normally follow you'll need the following function:

public Control FindControlRecursive(Control container, string name)
{
    if (container.ID == name)
        return container;

    foreach (Control ctrl in container.Controls)
    {
        Control foundCtrl = FindControlRecursive(ctrl, name);
        if (foundCtrl != null)
            return foundCtrl;
    }
    return null;
}

Add a RowDataBound handler to your GridView with the following:

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
                LinkButton lnkEdit =
                    (LinkButton)FindControlRecursive(e.Row, "lnkEdit");
               
               lnkEdit.CommandArgument = e.Row.RowIndex.ToString();
        }

Then in your RowCommand handler, where the commandname matches your edit command string:

YourGridView.EditIndex = Convert.ToInt32(e.CommandArgument);

hope this helps
0
 

Author Comment

by:R8VI
Comment Utility
Hi, I am sorry I am really struggling with this is there somewhere I can follow a full tutorial
for example did you man RowDataBound  or RowCommand as if cant find e.command name in Rowdatabound
0
 

Author Comment

by:R8VI
Comment Utility
this is what i have so far

    protected void RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditCommand")
            {
                GridViewRow CurrentRow = grdAgentSpecficProperty.SelectedRow;
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    LinkButton lnkEdit = (LinkButton)FindControl("lnkEdit");
                    int rowIndex = Convert.ToInt32(lnkEdit.CommandArgument);
                    grdAgentSpecficProperty.EditIndex = rowIndex;
                }

                
            }



 <asp:LinkButton ID="lnkEdit" runat="server" CssClass="EditButton" CommandName="EditCommand" CommandArgument='<%# Container.DataItemIndex %>'>Edit</asp:LinkButton>
        }

Open in new window

0
 

Author Comment

by:R8VI
Comment Utility
Hi,

having thought about this there are other places on the site that I have a link button and they dont work either so maybe trying to get to the bottom of why they dont work is a better solution.
I am not sure if you have had time to investigate on it at the moment ?

Thanks,r8VI
0
 
LVL 7

Expert Comment

by:markmiddlemist
Comment Utility
for the RowDatabound you should just need to create the event handler and post the code in I posted above:

        if (e.Row.RowType == DataControlRowType.DataRow)
        {
                LinkButton lnkEdit =
                    (LinkButton)FindControlRecursive(e.Row, "lnkEdit");
               
               lnkEdit.CommandArgument = e.Row.RowIndex.ToString();
        }

I've been trying to find out what problems other people have had in FireFox and have yet to find anything useful - I find it very hard to believe that you're the first person to ever have this problem.

It might be an idea to also post your issue up on the forums at asp.net - a lot of people with more specialised knowledge than me, including microsoft employees and MVPs monitor those boards

Sorry I can't be more help
0
 

Author Comment

by:R8VI
Comment Utility
Hi,

Thanks for this quick question this is what I am doing

        public Control FindControlRecursive(Control container, string name)
        {
            if (container.ID == name)
                return container;

            foreach (Control ctrl in container.Controls)
            {
                Control foundCtrl = FindControlRecursive(ctrl, name);
                if (foundCtrl != null)
                    return foundCtrl;
            }
            return null;
        }


protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            int iCountPages = grdAgentSpecficProperty.PageCount;
            int iCurrentPage = grdAgentSpecficProperty.PageIndex + 1;


            lblTotalPageCount.Text = "Page " + Convert.ToString(iCurrentPage) + " of " + Convert.ToString(iCountPages);

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
               /* LinkButton lnkEdit =
                    (LinkButton)FindControlRecursive(e.Row, "lnkEditButton");*/
                int iCountNumberofRows = grdAgentSpecficProperty.Rows.Count;
                int i;

                if (iCountNumberofRows == 0)
                {
                    LinkButton lnkEditButton = (LinkButton)grdAgentSpecficProperty.Rows[0].FindControl("lnkEditButton");
                    lnkEditButton.CommandArgument = e.Row.RowIndex.ToString();
                }
                else
                {
                    for (i = 0; i < iCountNumberofRows; i++)
                    {

                        LinkButton lnkEditButton = (LinkButton)grdAgentSpecficProperty.Rows[i].FindControl("lnkEditButton");
                        lnkEditButton.CommandArgument = e.Row.RowIndex.ToString();
                    }
                }
            }

        }

      protected void RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditMode")
            {
                grdAgentSpecficProperty.EditIndex = Convert.ToInt32(e.CommandArgument);
                GetPropertiesInfo();
                
            }
}

Open in new window


But my problem now is that how do i trap if use has clicked on the first row ?
I tried
                LinkButton lnkEditButton = (LinkButton)grdAgentSpecficProperty.Rows[0].FindControl("lnkEditButton");
                    lnkEditButton.CommandArgument = e.Row.RowIndex.ToString();

but that gives me index out of range

Thanks,

R8VI
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now