locked
edit and delete items in gridView (xmlDataSource) RRS feed

  • Question

  • User333908363 posted


    hi,

    we are trying to delete a specific row when clicking a button (each row have it s own delete button). 

    unfortunately we are not successful in doing so. 

    the program is  written in C#, Jquery and JS so we can use any of these language to delete. 

    the delete action need to update the grid and the XML code. 

    right now, for a different action, we prevented the grid from doing postback.

    here is the grid code: 

     <asp:ScriptManager ID="ScriptManager1" runat="server" />
                    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
                       
        <ContentTemplate>
                <asp:GridView ID="GridViewTrueAns" runat="server" AutoGenerateColumns="False" Width="221px" DataSourceID="XmlDataSourceTrueAns" Height="79px" OnRowCommand="GridViewTrueAns_RowCommand"  AutoPostBack="falsh">
                    <RowStyle CssClass="gridrowT" />
                    <Columns>
                        
                      <asp:TemplateField HeaderText="answer">
                            <ItemTemplate>
                               
                                <asp:TextBox ID="changeTrueAns" MaxLength="50" CharacterForLabel="AnsLimitChar" CssClass="MaxChar" runat="server" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" ></asp:TextBox>
                                <asp:Label ID="AnsLimitChar" runat="server" Text="0/50" CssClass="countChar"  Visible="false"></asp:Label> 
    
                               
    
    
                                <asp:Image Height="50px" ID="trueImage" runat="server" ImageUrl='<%#"~/uploadedFiles/"+XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" />
                                 <asp:Label ID="showAsnlbl" runat="server" Text='<%#XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" ></asp:Label>
                                      <label>                          
                                    <image ID="imagetest"   Height="200px" Width="200px" BorderStyle="Solid" BorderColor="Black"  class="imagetestClass" AutoPostBack="false" EnableViewState="false"></image>
                               
                                </label>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="resize img">
                            <ItemTemplate>
                                <asp:ImageButton ID="TbigPicBtn" runat="server" ImageUrl="~/editorImages/bigPic.jpg" CommandName="largeImg" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="edit answer">
                            <ItemTemplate>
                                <asp:ImageButton ID="cancelTrueAnsChange" runat="server" ImageUrl="~/editorImages/Exit.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="cancelEdit"/>
                                <asp:ImageButton ID="approveTrueAnsChange" runat="server" ImageUrl="~/editorImages/tick.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="approveEdit"/>
                                <asp:ImageButton ID="TeditPancilBtn" runat="server" ImageUrl="~/editorImages/edit.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' TheItemRow='<%#Container.DataItemIndex%>' CommandName="editRow" />
                                <asp:ImageButton ID="editTruePic" runat="server" ImageUrl="~/editorImages/edit.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' TheItemRow='<%#Container.DataItemIndex%>' OnClientClick="openFileUploader2(); return false; " />
                                <asp:ImageButton ID="approveTruePicChange" runat="server" ImageUrl="~/editorImages/tick.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="approveTruePicEdit"/>
    
                                </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="delete answer">
                            <ItemTemplate>
                                
                                <asp:ImageButton ID="TremoveAns" runat="server" ImageUrl="~/editorImages/bin.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' CommandName="deleteRow" />
                               
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </ContentTemplate>

    Sunday, July 12, 2020 12:46 PM

All replies

  • User1535942433 posted

    Hi MayAndOri,

    unfortunately we are not successful in doing so. 

    Could you tell us what the errors you have?As far as I think,you could use onRowCommand to delete the specific.Besides,you must node the itemindex and DataKeyNames.

    More details,you could refer to below article:

    https://forums.asp.net/t/1689577.aspx?GridView+and+XML+update+insert+delete+

    You could post your current full codes including of the code-behind and xml file to us.It will help us to solve your problem.

    Best regards.

    Yijing Sun

    Monday, July 13, 2020 6:49 AM
  • User333908363 posted

    Hi,

    Thank you for your quick answer.

    we want to upload a image in the grid view  and after the user click the tick sign it will update the Xml File.

    we tried to do this with upload panel but it massing up the other image buttons like the delete or resize button to resize the image. 

    here is a link to the code: https://drive.google.com/file/d/11v0GwlbgPj08EHbOUPtuke0uNXO3mDeu/view?usp=sharing 

    thank you so much!!

    Monday, July 13, 2020 7:30 AM
  • User1535942433 posted

    Hi MayAndOri,

    Accroding to your description,I couldn't preview your codes.I suggest you could post the code to the github,and post the link to us.

    Best regards,

    Yijing Sun

    Monday, July 13, 2020 9:38 AM
  • User348806598 posted

    Hi MayAndOri,

    Welcome to forums.asp.net.

    If the buttons those not working is outside of the update panel, then try using conditional UpdateMode like in the following example(Listing 1)-

    https://docs.microsoft.com/en-us/aspnet/web-forms/overview/older-versions-getting-started/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers#walkthrough-cross-updatepanel-triggers

    Monday, July 13, 2020 10:02 AM
  • User333908363 posted

    thank you!

    We tried to insert a trigger to the image button but it doesn't know the Id of the button.

                <div class="gridDiv" id="gridTrueDiv">
                     <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true"/>
                    <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
                     <ContentTemplate>
    
                <asp:GridView ID="GridViewTrueAns" runat="server" AutoGenerateColumns="False" Width="221px" DataSourceID="XmlDataSourceTrueAns" Height="79px" OnRowCommand="GridViewTrueAns_RowCommand">
                    <Columns>
                      <asp:TemplateField HeaderText="תשובה">
                       <ControlStyle width="250px"/>
                            <ItemTemplate> 
                                <asp:TextBox ID="changeTrueAns" MaxLength="50"  CharacterLimit="50" CharacterForLabel="AnsLimitChar" CssClass="maxText" runat="server" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" ></asp:TextBox>
                                <asp:Panel ID="Panel1" runat="server" Width="90">
                                <asp:Image ID="trueImage" runat="server" ImageUrl='<%#"~/uploadedFiles/"+XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" Width="90px" />
                                </asp:Panel>
                                 <asp:Label ID="showAsnlbl" TheItemRow='<%#Container.DataItemIndex%>' runat="server" Text='<%#XPathBinder.Eval(Container.DataItem, "@isCorrect/..").ToString()%>' Visible="false" ></asp:Label>
                                      <label>                          
                                    <image ID="imagetest"   Height="200px" Width="200px" BorderStyle="Solid" BorderColor="Black"  class="imagetestClass" AutoPostBack="false" EnableViewState="false"></image>
                               
                                </label>
                            </ItemTemplate>
                        </asp:TemplateField>
    
                        <asp:TemplateField HeaderText="RESIZE PIC">
                            <ItemTemplate>
                                <asp:ImageButton ID="TbigPicBtn" runat="server" ImageUrl="~/editorImages/bigPic.jpg" CommandName="largeImg" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>'/>
                            </ItemTemplate>
                        </asp:TemplateField>
    
    
                        <asp:TemplateField HeaderText="EDIT">
                            <ItemTemplate>
                                <asp:ImageButton ID="cancelTrueAnsChange" runat="server" ImageUrl="~/editorImages/Exit.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="cancelEdit"/>
                                <asp:ImageButton ID="approveTrueAnsChange" runat="server" ImageUrl="~/editorImages/tick.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="approveEdit"/>
                                <asp:ImageButton ID="TeditPancilBtn" runat="server" ImageUrl="~/editorImages/edit.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' TheItemRow='<%#Container.DataItemIndex%>' CommandName="editRow" />
                                <asp:ImageButton ID="editTruePic" runat="server" ImageUrl="~/editorImages/edit.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' TheItemRow='<%#Container.DataItemIndex%>' OnClientClick="openFileUploader2(); return false; " />
                                <asp:ImageButton ID="approveTruePicChange" runat="server" ImageUrl="~/editorImages/tick.png" theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' Visible="false" CommandName="approveTruePicEdit"/>
                                </ItemTemplate>
                        </asp:TemplateField>
    
    
                        <asp:TemplateField HeaderText="DEL" >
                            <ItemTemplate >
                                <asp:ImageButton ID="TremoveAns" runat="server" ImageUrl="~/editorImages/bin.jpg"  theAnsId='<%#XPathBinder.Eval(Container.DataItem, "@ansId").ToString()%>' CommandName="deleteRow" AutoPostBack="True"/>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                      
                </asp:GridView>
            </ContentTemplate>
    
         <Triggers>
         <asp:PostBackTrigger ControlID="TremoveAns"  />
        </Triggers>
                        
           </asp:UpdatePanel>

    Monday, July 13, 2020 10:24 AM
  • User348806598 posted

    Hi,

    TremoveAns button is already inside gridview and update panel. I was talking about any control that is outside of the update panel

    Monday, July 13, 2020 2:41 PM
  • User333908363 posted

    We figure the problem.. thank you.

    Monday, July 13, 2020 3:54 PM