Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello fellow developer, the catch is I am developing a page wherein there is a nested user control inside my gridview(grdCategory). So Ive manage to bind it programically(woking no doubt) using Sub that I could call anytime.
Using Parent-child(usercontrol) hierarchy, the user control which has a gridview that respond on the ID of the grdCategory My code are as follows
VB
Private Sub CreateCourseGrid(Optional ByVal parentIsPostBack As Boolean = False)
        For iCatRow As Integer = 0 To grdCategory.Rows.Count - 1
            Dim ccId As String = _
            DirectCast(grdCategory.Rows(iCatRow).Cells(1).FindControl("lblCourseCatIDMain"), Label).Text
            Dim year As String = drpdwnYear.Text
            Dim disciplineId As String = "0"

            _coursesdataDAL = New CoursesDataDAL
            Dim coursedata As New CoursesData
            coursedata.Year = year
            coursedata.CourseCatID = ccId
            coursedata.DisciplineID = disciplineId

            Dim pnlCourse As Panel = DirectCast(grdCategory.Rows(iCatRow).Cells(1).FindControl("pnlCourse"), Panel)
            Dim courseUC As CourseUserControl = LoadControl("~/CourseUserControl.ascx")
            courseUC.CData = coursedata
            courseUC.MyParent = CourseUserControl.ParentType.TrainingPlanTemplate
            'e.Row.Cells(1).Controls.Add(courseUC)
            pnlCourse.Controls.Add(courseUC)
        Next

    End Sub


So my error goes here:
I created a javascript: validate on rowdatabound of my gridview(grdCategory) that contains the other clientID that is inside of its template
VB
Dim txtID As TextBox = DirectCast(e.Row.FindControl("txtCourseCatID"), TextBox)
            Dim txttry As TextBox = DirectCast(e.Row.FindControl("txtCourseCategory"), TextBox)
            Dim labeltry As Label = DirectCast(e.Row.FindControl("lbllastname"), Label)
            labeltry.Attributes.Add("onclick", "javascript:return validate('" + labeltry.ClientID + "','" + txttry.ClientID + "','" & txtID.ClientID & "')")


and on my client side here is my code
C#
function validate(linkobj, textobj, labelobj) {
          var textme = document.getElementById('hidIsChildEditMode').value;
          textme = textme + 1
          document.getElementById('hidIsChildEditMode').value = textme
          if (textme == 11) {
              document.getElementById('ctl00_ContentPlaceHolder1_grdCategory_ctl02_ctl02_ButtonDELL').click();
              //alert("wew")
              var CategoryID = document.getElementById(labelobj).value;
              document.getElementById('<%= txt1.ClientID %>').value = CategoryID

              document.getElementById('hidlinkObj').value = linkobj;
              document.getElementById(linkobj).style.display = 'none';

              document.getElementById('hidtextObj').value = textobj;
              document.getElementById(textobj).style.display = 'inline';
          }
          else {
              document.getElementById(linkobj).style.display = 'inline';
              document.getElementById(textobj).style.display = 'none';
          }


      }

Note that there is a buttonclickDELL that is clicked which is really important since it rebinds the usercountrol inside. The buttonDELL is inside the usercontrol and declared as default button, Code are as follow on client side of my user control
XML
<asp:Panel ID="Panel1" runat="server" DefaultButton="ButtonDELL">
    <asp:GridView ID="grdCourse" runat="server" Width="800px" HorizontalAlign="Center"
        AutoGenerateColumns="False" align="center" DataKeyNames="TrainingPlanID,CourseID"
        Font-Names="verdana,arial,helvetica,sans-serif" Font-Size="8pt" BackColor="White"
        GridLines="Vertical" OnRowCommand="grdCourse_RowCommand">
        <Columns>
            <asp:ButtonField Text="SingleClick" CausesValidation="false" CommandName="SingleClick"
                Visible="False" />
            <asp:TemplateField HeaderText="TrainingPlanID" SortExpression="TrainingPlanID" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblTrainingPlanID" runat="server" Text='<%# Eval("TrainingPlanID")%>'
                        Width="30px" Height="10px"> </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Course Name" SortExpression="CourseName" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblCoursename" runat="server" Text='<%# Eval("CourseName")%>' Width="300px"
                        Height="10px"> </asp:Label>
                    <asp:TextBox ID="txtCourseName" runat="server" Text='<%# Bind("CourseName") %>' Font-Size="XX-Small"
                        Visible="false" Font-Names="Verdana" CausesValidation="false" OnTextChanged="txtCourseName_TextChanged"
                        Width="300px" ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" ErrorMessage="*"
                        ControlToValidate="txtCourseName" Font-Names="verdana" ForeColor="white" Font-Size="XX-Small"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Course Code" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblCourseCode" Width="120px" runat="server" Text='<%# Eval("CourseCode")%>'
                        Height="10px"> </asp:Label>
                    <asp:TextBox ID="txtCourseCode" Width="120px" runat="server" Text='<%# Eval("CourseCode") %>'
                        Font-Size="XX-Small" Visible="false" Font-Names="Verdana" CausesValidation="false"
                        OnTextChanged="txtCourseName_TextChanged"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator100" runat="server" ErrorMessage="*"
                        ControlToValidate="txtCourseCode" Font-Names="verdana" ForeColor="white" Font-Size="XX-Small"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Duration" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblDuration" Width="100px" runat="server" Text='<%# Eval("Duration")%>'
                        Height="10px"> </asp:Label>
                    <asp:TextBox ID="txtDuration" Width="100px" runat="server" CausesValidation="false"
                        OnTextChanged="txtCourseName_TextChanged" Text='<%# Eval("Duration")%>' Font-Size="XX-Small"
                        Visible="false" Font-Names="Verdana"></asp:TextBox>
                    <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtDuration"
                        Font-Names="verdana" ForeColor="white" Font-Size="XX-Small" ErrorMessage="No/Dec only"
                        ValidationExpression="(^\d*\.?\d*[1-9]+\d*$)|(^[1-9]+\d*\.\d*$)"></asp:RegularExpressionValidator>
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*"
                        ControlToValidate="txtDuration" Font-Names="verdana" ForeColor="white" Font-Size="XX-Small"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Edit Detail" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:ImageButton ID="EditDetail" Height="15px" ImageUrl="Images/edit2.png" runat="server"
                        CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' PostBackUrl='<%#"TrainingPlanDetails.aspx?TPID=" & Eval("TrainingPlanID")%>' />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Delete" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageDeleteCourse" Height="15px" ImageUrl="Images/Delete.png"
                        runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("TrainingPlanID") + ";" + Eval("CourseID") %>'
                        OnClientClick="return confirm('Are you sure to delete this Course?')" /><!-- onclick="ImageDeleteCourse_Click" -->
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:CommandField ShowDeleteButton="True" />
            <asp:TemplateField HeaderText="DisciplineID" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="DisciplineCourseID" runat="server" Text='<%# Eval("DisciplineID")%>'
                        Height="10px"> </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="EditCourse" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:ImageButton ID="ImageEdit" Height="15px" ImageUrl="Images/edit.png" runat="server"
                        CommandName="cmdlink" CommandArgument='<%# Eval("TrainingPlanID")%>' />
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CourseID" SortExpression="CourseID" HeaderStyle-HorizontalAlign="Left">
                <ItemTemplate>
                    <asp:Label ID="lblCourseID" runat="server" Text='<%# Eval("CourseID")%>' Width="30px"
                        Height="10px"> </asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <RowStyle BackColor="White" ForeColor="#333333" />
        <FooterStyle BackColor="White" ForeColor="#333333" />
        <PagerStyle BackColor="#336666" ForeColor="White" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
    </asp:GridView>
    <asp:Button ID="ButtonDELL" runat="server" Text="Button" style="display:none;" CausesValidation="false" CommandName="defaultButton"
         />
</asp:Panel>

So overall when I click the the label of my grdCategory it must change to textbox but before that the user control must rebind (in which case I use default button to rebind)
Here is my huge problem:
using the javascript validate when I add alert It is working fine but when I try to remove it, events turn out what I did not want to happen, that is client side first before server side. So reading my code I found out that the error occurs when the buttonDELL was clicked because it rebinds and I think it returns to his original state.
My trial is putting it to a hidden input field(hidlinkObj,hidtextObj) and call it on ButtonDELL_clicked on server side but nothing works.Can you help me sort this out?
My trial on ButtonDELL_clicked
ASM
Protected Sub ButtonDELL_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonDELL.Click
     ScriptManager.RegisterStartupScript(Me, Me.GetType, "Any", "document.getElementById('hidlinkObj').style.display = 'none';;", True)
     ScriptManager.RegisterStartupScript(Me, Me.GetType, "Any2", "document.getElementById(hidtextobj).style.display = 'inline';", True)
 End Sub
Posted

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