简体   繁体   中英

ASP.NET C# signature canvas in editing gridview

i'm new to stackoverflow, if I miss anything don't hesitate to comment.

So, here's my problem.

I'm trying to include a signature canvas in my editable gridview. I tried many different solution but the canvas was never working. I tried for once to put it out of the gridview, in my surprise, the signature was working. BUT, as soon as I clicked on the arrows for editing, the canvas stopped workin even tho it was out of the gridview.

Any idea why?

here's my code, I didn't include everything, if you need more please ask.

ps: everyting else in the gridview is working and showing without problem.

ASP.NET

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">


    <asp:UpdatePanel ID="Panel1" runat="server">
    <ContentTemplate> <%--runat="server">--%>

  <asp:GridView 
             ID="GridView2" 
             runat="server" 
             GridLines="None" 
             AutoGenerateColumns="False" 
             CellPadding="6" 
             CssClass="mGrid" 
             OnRowCancelingEdit="GridView2_RowCancelingEdit"   
             OnRowEditing="GridView2_RowEditing" 
             OnRowUpdating="GridView2_RowUpdating" 
             OnRowDeleting="GridView2_RowDeleting" 
             OnRowDataBound="GridView2_RowDataBound">  
            <Columns>                  
                <asp:TemplateField  HeaderText="Numéro">  
                    <ItemTemplate><asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label></ItemTemplate>  
                    <EditItemTemplate>  
                            <table  cellpadding="0" cellspacing="0" width="100%" width="100%">
                            <tr class="bottom" >
                                <td width="70px"><asp:Label ID="lbl_ID2" runat="server" Text='<%#Eval("ID") %>'></asp:Label></td>
                                <td width="205px"><asp:Label ID="lbl_User2" runat="server" Text='<%#Eval("User") %>'></asp:Label></td>
                                <td width="123px"><asp:Label ID="lbl_Dep2" runat="server" Text='<%#Eval("Departement") %>'></asp:Label></td>
                                <td width="52px"><asp:Label ID="lbl_Total2" runat="server" Text='<%#Eval("Total") %>'></asp:Label></td>
                                <td width="80px"><asp:Label ID="lbl_Date2" runat="server" Text='<%#Eval("Date") %>'></asp:Label></td>
                             </tr><tr class="spacer"><td></td></tr>
                            <tr>
                                <td class="support"></td>
                                <td colspan="4"><asp:GridView GridLines="None" CssClass="mGrid2" width="100%" ID="GridView" runat="server"></asp:GridView></td>
                            </tr>
                            <tr class="spacer">
                                <td></td>
                            </tr>
                            <tr>
                                <td colspan="2"><asp:Label ID="Lproject" runat="server" Text=""></asp:Label></td>
                                <td class="bottom" colspan="2" rowspan="4">                   
                                   <div class="tools">
                                   <a href="#colors_sketch" data-tool="marker">Marker</a> <a href="#colors_sketch" data-tool="eraser">Eraser</a>
                                   </div><br />
                                    <canvas id="colors_sketch" width="300px" height="100pxs"></canvas><asp:HiddenField ID="ImageVal" runat="server" />                   
                                </td>
                                <td></td>
                            </tr>
                            <tr>
                                <td colspan="2"><asp:Label ID="Lpaiement" runat="server" Text=""></asp:Label></td>
                                <td><asp:Button ID="btn_Update" runat="server" Text="Autoriser" CommandName="Update"/></td>
                            </tr>
                            <tr>
                                <td  colspan="2">Autorisé/Refuser par :</td>
                            </tr>
                            <tr class="bottom" >
                                <td colspan="2"><asp:TextBox ID="Tauth" runat="server"></asp:TextBox></td>               
                                <td><asp:Button ID="btn_Delete" runat="server" Text="Refuser" CommandName="Delete"/>  </td>
                            </tr>
                        </table>
                    </EditItemTemplate> 
                </asp:TemplateField >  
                <asp:TemplateField HeaderText="Soumis">  
                    <ItemTemplate><asp:Label  ID="lbl_User" runat="server" Text='<%#Eval("User") %>'></asp:Label></ItemTemplate>  
                </asp:TemplateField>  
                <asp:TemplateField HeaderText="Departement">  
                    <ItemTemplate>  <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Departement") %>'></asp:Label>  </ItemTemplate>  
                </asp:TemplateField>  
                <asp:TemplateField HeaderText="Total">  
                    <ItemTemplate><asp:Label ID="lbl_Total" runat="server" Text='<%#Eval("Total") %>'></asp:Label></ItemTemplate>  
                </asp:TemplateField> 
                <asp:TemplateField HeaderText="Date">  
                    <ItemTemplate><asp:Label ID="lbl_Date" runat="server" Text='<%#Eval("Date") %>'></asp:Label></ItemTemplate>  
                </asp:TemplateField> 
                <asp:TemplateField>  
                    <ItemTemplate><asp:Button ID="btn_Edit" runat="server" Text="↓" CommandName="Edit" /></ItemTemplate>  
                    <EditItemTemplate>  
                        <asp:Button ID="btn_Cancel" runat="server" Text="↑" CommandName="Cancel" />  
                    </EditItemTemplate>   
                    </asp:TemplateField>  
            </Columns>  
           <HeaderStyle />             
        </asp:GridView>

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script src="https://cdn.rawgit.com/mobomo/sketch.js/master/lib/sketch.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $('#colors_sketch').sketch();
                $(".tools a").eq(0).attr("style", "color:#000");
                $(".tools a").click(function () {
                    $(".tools a").removeAttr("style");
                    $(this).attr("style", "color:#000");
                });
                $("#btnSave").bind("click", function () {                
                    var base64 = $('#colors_sketch')[0].toDataURL();
                    $('[Id*=ImageVal]').val(base64);
                });
            });
        </script>

 </ContentTemplate>     
        </asp:UpdatePanel> 
    </div>

</asp:Content>

C#

protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowState.ToString().Contains("Edit"))
        {
            GridView editGrid = sender as GridView;
            int colSpan = editGrid.Columns.Count;
            for (int i = 1; i < colSpan - 1 ; i++)
            {
                e.Row.Cells[i].Visible = false;
                e.Row.Cells[i].Controls.Clear();
            }

            e.Row.Cells[0].Attributes["ColSpan"] = (colSpan - 1).ToString();
        }

    }

 protected void GridView2_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)
    {



        //NewEditIndex property used to determine the index of the row being edited.  
        GridView2.EditIndex = e.NewEditIndex;
        fillcomm(Ddepfilt.SelectedValue);
        GridView GV = GridView2.Rows[GridView2.EditIndex].FindControl("GridView") as GridView;
        Label id = GridView2.Rows[GridView2.EditIndex].FindControl("lbl_ID2") as Label;       
        Label project = GridView2.Rows[GridView2.EditIndex].FindControl("Lproject") as Label;
        Label paiement = GridView2.Rows[GridView2.EditIndex].FindControl("Lpaiement") as Label;

        string query2 = "select bdc_projectname, bdc_paiement from BDC where bdc_num = '" + id.Text + "'";
        string query = "select bdci_qty as Qty, bdci_item as Decription, bdci_price as Prix from BDCITEM inner join BDC on bdci_num = bdc_num where bdc_num = '"+id.Text+"'";
        SqlDataAdapter gridadapt = new SqlDataAdapter(query, conn);
        SqlDataAdapter detailsadapt = new SqlDataAdapter(query2, conn);
        DataSet currentgrid = new DataSet();
        DataSet details = new DataSet();

        conn.Open();

        gridadapt.Fill(currentgrid);
        detailsadapt.Fill(details);

        conn.Close();

        project.Text = "Projet : " + details.Tables[0].Rows[0][0].ToString();
        paiement.Text = "Méthode de paiement : " + details.Tables[0].Rows[0][1].ToString();

        GV.DataSource = currentgrid;
        GV.DataBind();




    }



  1. Everything you change in the DOM on the client-side, eg adding/changing stuff with javascript, will be lost, when you post back and don't tell the server about the changes in any way.
  2. When you use async-Postbacks, meaning UpdatePanels in your case, the document.ready -Event is only triggered once - when the Page is loaded the first time. Same goes for window.onload .
  3. Dont't put stuff like javascript-links in your UpdatePanel , that should be static and loaded only once.

So you see, why this is not working: When page is first loaded, the canvas is not rendered, because your GridView is not in edit-mode, I assume. So the document.ready -function can't init the canvas .

To get this working you need to init your canvas after switching to edit-mode, eg by adding a startup-script in the server-event, or by using async-page-load-events in javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM