locked
Databind not executed, why? RRS feed

  • Question

  • User-909867351 posted

    Hi

    I have one button inside a bootstrap panel and I want to insert one record. here is my code:

    <div class="panel-footer">
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:Button ID="Button4" runat="server" Text="Adicionar" OnClick="Adiciona_Click" />
                                </ContentTemplate>                            
                            </asp:UpdatePanel>
    
                            
                        </div>

    and my c# code:

     protected void Adiciona_Click(object sender, EventArgs e)
        {
            
            foreach (GridViewRow row in GridView2.Rows)
            {
                CheckBox chkbox = (CheckBox)row.FindControl("CheckBox2");
                if (chkbox.Checked == true)
                {
                    SqlDataSource1.InsertParameters.Clear();
                    SqlDataSource1.InsertCommand = "insert into turmas (designacao,aluno_id,nome_aluno,id_prof,nome_prof) values (?,?,?,?,?)";
                    SqlDataSource1.InsertParameters.Add("@a", lblTurma.Text);
                    SqlDataSource1.InsertParameters.Add("@a", row.Cells[1].Text);
                    SqlDataSource1.InsertParameters.Add("@a", Server.HtmlDecode(row.Cells[2].Text));
                    SqlDataSource1.InsertParameters.Add("@a", "1");
                    SqlDataSource1.InsertParameters.Add("@a", "Professor de teste");
                    SqlDataSource1.Insert();
                }
            }
                
            SqlDataSource2.DataBind();
            GridView1.DataBind();        
            ModalPopupExtender1.Hide();
        }

    My problem:

    The record is inserted but the databind are not executed. Why?

    thank you

    Monday, July 16, 2018 2:10 PM

Answers

  • User-1171043462 posted

    You can use multiple UpdatePanels on Page. Put the Button and GridView both inside UpdatePanel either same or two different

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 8:40 AM

All replies

  • User-492460945 posted

    Can you post ASPX code related to gridview and datasource?

    Monday, July 16, 2018 3:12 PM
  • User-1171043462 posted

    Is your GridView and SqlDataSource inside UpdatePanel?

    If not then please put as putting Button inside UpdatePanel does the Task. But it does not refresh other parts of Page. It only refreshes what's inside UpdatePanel

    Monday, July 16, 2018 8:04 PM
  • User-909867351 posted

    Hi

    No, the gridview is not inside the update panel but how can put it inside the updatePanel, is in other part of the page? What you mean about putting Button?

    here is all my page (I want to refresh Gridview1 and I think I don't need to do sqldatasource2.refresh):

    <%@ Page Title="" Language="C#" MasterPageFile="professor.master" AutoEventWireup="true" CodeFile="todas_turmas.aspx.cs" Inherits="professor_todas_turmas" %>
    
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
    
    <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
        <link href="../bootstrap/bootstrap.css" rel="stylesheet" />  
        <style>
    .modalBackground
    {
        background-color:Gray;
        filter:alpha(opacity=50);
        opacity:0.7;
    }
    .pnlBackGround
    {
     ;
        top:10%;
        left:10px;
        /*width:300px;
        height:125px;*/
        text-align:center;
        background-color:White;
        border:solid 3px black;
    }
        </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
        <form runat="server">
            <div class="container">
                <div class="row">
                    <div class="col-md-4">
                        <div class="page-header"><h2>Todas as turmas</h2></div>
                        <asp:ListBox ID="ListBox1" CssClass="form-control" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="designacao" DataValueField="designacao" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged"></asp:ListBox>
                    </div>
                    <div class="col-md-6">
                        <div class="page-header"><h2>Alunos da turma <small><asp:Label ID="lblTurma" runat="server" Text="Label"></asp:Label></small></h2></div>
                            
                        <asp:GridView ID="GridView1" runat="server" CssClass="table table-hover table-striped" GridLines="None" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:CheckBox ID="CheckBox1" runat="server" Checked="True" CssClass="form-control" />
                                    </ItemTemplate>
                                </asp:TemplateField>
                                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" SortExpression="id" />
                                <asp:BoundField DataField="aluno_id" HeaderText="aluno_id" SortExpression="aluno_id" />
                                <asp:BoundField DataField="nome_aluno" HeaderText="nome_aluno" SortExpression="nome_aluno" />
                                <asp:BoundField DataField="id_prof" HeaderText="id_prof" SortExpression="id_prof" />
                                <asp:BoundField DataField="nome_prof" HeaderText="nome_prof" SortExpression="nome_prof" />
                            </Columns>
                        </asp:GridView>
                    </div>
                    <div class="col-md-2">
                        <div class="page-header"><h2>Opções</h2></div>
                        <div class="form-group">
                            <asp:Button ID="Button1" runat="server" CssClass="btn-info form-control" Text="Atualizar" OnClick="Atualizar_Click" />
                        </div>
                        <div class="form-group">
                            <asp:Button ID="Button2" runat="server" CssClass="btn-success form-control" Text="Adicionar Aluno" OnClick="Adicionar_Aluno" />
                        </div>
                    </div>
                </div>
            </div>
    <%--lugar dos panels--%>
            <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
            <ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" TargetControlID="Label3" PopupControlID="todosalunos" CancelControlID="Button3" BackgroundCssClass="modalBackground" DropShadow="True" runat="server"></ajaxToolkit:ModalPopupExtender>
    <div class="container">
        <div class="row">
            <asp:Label ID="Label3" runat="server" Text="" ></asp:Label>
            <div class="col-md-12 ">
                <div class="panel pnlBackGround" id="todosalunos" style="display: none;">
                    <div class="panel panel-info ">
                        <div class="panel-heading">
                            <div class="row">
                                <div class="col-md-6">
                                    Todos os alunos
                                </div>
                                <div class="col-md-6 text-right">
                                    <asp:Button ID="Button3" CssClass="btn-sm" runat="server" Text="X" />
                                </div>
                            </div>
                           
                        </div>
                        <div class="panel-body">
                            <asp:GridView ID="GridView2" CssClass="table table-hover table-striped" GridLines="None" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource3">
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:CheckBox ID="CheckBox2" runat="server" CssClass="form-control" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" SortExpression="id" />
                                    <asp:BoundField DataField="nome" HeaderText="nome" SortExpression="nome" />
                                </Columns>
                            </asp:GridView>
                        </div>
                        <div class="panel-footer">
                            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                    <asp:Button ID="Button4" runat="server" Text="Adicionar" OnClick="Adiciona_Click" />
                                    
                                </ContentTemplate>                            
                            </asp:UpdatePanel>                        
                        </div>
                    </div>
                    </div>        
                </div>
    </div>
    </div>
    
        </form>
            <%--fim do codigo--%>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select distinct designacao from turmas order by designacao desc"></asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select * from turmas where designacao=?">
            <SelectParameters>
                <asp:ControlParameter ControlID="ListBox1" Name="?" PropertyName="SelectedValue" DefaultValue="99" />
            </SelectParameters>
        </asp:SqlDataSource>
        <asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="select * from alunos order by nome"></asp:SqlDataSource>
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </asp:Content>

    Monday, July 16, 2018 10:00 PM
  • User-1171043462 posted

    You can use multiple UpdatePanels on Page. Put the Button and GridView both inside UpdatePanel either same or two different

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 17, 2018 8:40 AM