如何使用ajax绑定gridview?

时间:2018-01-15 14:02:05

标签: asp.net

我有ASPX应用程序。我无法使用Ajax绑定gridview。 Ajax将gridview的标题行返回为datarow。

我的Ajax代码:

function bankmodulhargetir() {

// Bank modul values

var bankmodulvalues = {};

bankmodulvalues.bankmodulnom = $("#bankmodulnom").val();
bankmodulvalues.bankmodultarix = $("#bankmodultarix").val();
bankmodulvalues.bankmodulverenbankkod = $("#bankmodulverenbankkod").val();
bankmodulvalues.bankmodulherekettip = $('#bankmodulherekettip input[type=radio]:checked').val();
bankmodulvalues.bankmodulesastip = $("#bankmodulesastip").val();
bankmodulvalues.bankmodulsirket = $("#bankmodulsirket").val();
bankmodulvalues.bankmodulstatus = $("#bankmodulstatus").val();
bankmodulvalues.bankmodulqeyd = $("#bankmodulqeyd").val();
bankmodulvalues.bankmodulcemvalyuta = $("#bankmodulcemvalyuta").val();
bankmodulvalues.bankmodulcemvalmebleg = $("#bankmodulcemvalmebleg").val();
bankmodulvalues.bankmodulcemmebleg = $("#bankmodulcemmebleg").val();
bankmodulvalues.bankmodullayihe = $("#bankmodullayihe").val();
bankmodulvalues.bankmodulherekettip2 = $("#bankmodulherekettip2").val();
bankmodulvalues.bankmodulmusterikod = $("#bankmodulmusterikod").val();
bankmodulvalues.bankmodulesaskod = $("#bankmodulesaskod").val();
bankmodulvalues.bankmodulesaskod2 = $("#bankmodulesaskod2").val();
bankmodulvalues.bankmodulvalyuta = $("#bankmodulvalyuta").val();
bankmodulvalues.bankmodulkurs = $("#bankmodulkurs").val();
bankmodulvalues.bankmodulvalmebleg = $("#bankmodulvalmebleg").val();
bankmodulvalues.bankmodulmebleg = $("#bankmodulmebleg").val();
bankmodulvalues.bankmodultertibeden = $("#bankmodultertibeden").val();
bankmodulvalues.bankmodultertibtarixi = $("#bankmodultertibtarixi").val();

var bankmoduljsonData = JSON.stringify({
    bankmodulvalues: bankmodulvalues
});

$.ajax({
    type: "POST",
    url: "Main.aspx/bankmodulhargetirstatic",
    data: bankmoduljsonData,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: OnSuccess,
    failure: function (response) {
        alert(response.d);
    },
    error: function (response) {
        alert(response.d);
    }
});


function OnSuccess(response) {
    var xmlDoc = $.parseXML(response.d);
    var xml = $(xmlDoc);
    var customers = xml.find("Table");
    var row = $("[id*=bankmodulgrid] tr:last-child").clone(true);
    $("[id*=bankmodulgrid] tr").not($("[id*=bankmodulgrid] tr:first-child")).remove();
    $.each(customers, function () {
        var customer = $(this);
        $("td", row).eq(0).html($(this).find("harid").text());
        $("td", row).eq(1).html($(this).find("compbankhartarix").text());
        $("td", row).eq(2).html($(this).find("carikartad").text());
        $("td", row).eq(3).html($(this).find("compbankharesaskod").text());
        $("td", row).eq(4).html($(this).find("compbankharesas2kod").text());
        $("td", row).eq(5).html($(this).find("compbankharvalyuta").text());
        $("td", row).eq(6).html($(this).find("compbankharvalmebleg").text());
        $("[id*=bankmodulgrid]").append(row);
        row = $("[id*=bankmodulgrid] tr:last-child").clone(true);
    });
}



}

我的HTML代码

<div class="w3-row w3-section">
                            <div class="w3-rest">
                        <asp:Button id="bankmodulelaveet" runat="server" Text="Siyahıya əlavə et" Height="30px" Width="100%" Font-Size="Small" OnClientClick="bankmodulhargetir();return false;"   CssClass="w3-btn w3-white w3-border w3-border-gray w3-left w3-hover-blue"></asp:Button>

                                </div>
                                    </div> <br />

                       <div class="w3-responsive">






                        <asp:GridView ID="bankmodulgrid" runat="server"  AutoGenerateColumns="false" Width="100%" CssClass="w3-table-all w3-left w3-responsive"  ShowHeaderWhenEmpty="true" HeaderStyle-Font-Size="12px" DataKeyNames="harid" RowStyle-ForeColor="Black" RowStyle-Font-Bold="true"
         OnRowDeleting="bankmodulgrid_RowDeleting"
         OnRowDataBound="bankmodulgrid_RowDataBound" >
        <Columns>


            <asp:BoundField DataField="harid" HeaderText="№" Visible="true" ItemStyle-CssClass="harid"   />



            <asp:BoundField DataField="compbankhartarix" HeaderText="Tarix" ControlStyle-CssClass="compbankhartarix" >
                <HeaderStyle Width="15%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>

            <asp:BoundField DataField="carikartad" HeaderText="Müştəri" ControlStyle-CssClass="carikartad" >
                <HeaderStyle Width="25%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>

            <asp:BoundField DataField="compbankharesaskod" HeaderText="Əsas-1" ControlStyle-CssClass="compbankharesaskod"  >
                <HeaderStyle Width="20%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>

            <asp:BoundField DataField="compbankharesas2kod" HeaderText="Əsas-2" ControlStyle-CssClass="compbankharesas2kod" >
                <HeaderStyle Width="20%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>



            <asp:BoundField DataField="compbankharvalyuta" HeaderText="Valyuta" ControlStyle-CssClass="compbankharvalyuta"  >
                <HeaderStyle Width="10%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>

            <asp:BoundField DataField="compbankharvalmebleg" HeaderText="Məbləğ" ControlStyle-CssClass="compbankharvalmebleg" >
                <HeaderStyle Width="10%" />
                <ItemStyle Height="5px" Font-Size="12px"/>
            </asp:BoundField>

            <asp:TemplateField HeaderText="Əməliyyatlar"  >
               <ItemStyle Height="5px" Font-Size="10px"/>

                 <ItemTemplate >

              <asp:ImageButton ID="ButtonDelete" runat="server" CommandName="Delete" Text='Delete'  ImageUrl="Images/Symbol-Delete.png" />
                 </ItemTemplate>
              </asp:TemplateField>


        </Columns>

    </asp:GridView>

                           </div>

我的c#:

[的WebMethod]     public static string bankmodulhargetirstatic(bankmodulvalues bankmodulvalues)     {         modulkod =“FIN102”;         nomreyoxla = bankmodulvalues.bankmodulnom;

    if (HttpContext.Current.Session["sistemsenediyadd"] != null)

    {
        sistemsenedid = HttpContext.Current.Session["sistemsenediyadd"].ToString();
    }



    string query = "select har.HereketID as harid, ck.carikartad,format(har.compbankhartarix,'dd-MM-yyyy') as compbankhartarix,har.compbankharvalyuta,har.compbankharvalmebleg,har.compbankharesaskod,har.compbankharesas2kod  " +
        " from compbankhar har with (Nolock) " +
        "  left outer join carikart ck with (nolock) on ck.carikartkod = har.compbankharmusterikod " +
        "  where har.compbankharkod = '" + nomreyoxla + "' and har.compbankharsistemid = '" + sistemsenedid + "'";
    SqlCommand cmd = new SqlCommand(query);
    return GetData(cmd).GetXml();
}
private static DataSet GetData(SqlCommand cmd)
{
    string strConnString = ConfigurationManager.ConnectionStrings["JAMPROCONNECT"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (DataSet ds = new DataSet())
            {
                sda.Fill(ds);
                return ds;

            }
        }
    }
}

不起作用

Screen

Screen

1 个答案:

答案 0 :(得分:0)

我发现问题,就是这段代码。

public void BindHeaderRowInGridview()
    {
        DataTable dtDummy = new DataTable();
        DataColumn column;
        foreach (DataControlField field in bankmodulgrid.Columns)
        {
            column = new DataColumn();
            if (field is BoundField)
            {
                BoundField bfield = field as BoundField;
                column.ColumnName = (field as BoundField).DataField;
                dtDummy.Columns.Add(column);
            }

        }

        dtDummy.Rows.Add();
        bankmodulgrid.DataSource = dtDummy;
        bankmodulgrid.DataBind();

    }

 protected void Page_load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            this.BindHeaderRowInGridview();
        }

    }

BindHeaderRowInGridview()在我的应用中使用!ispostback在pageload中不起作用。我将BindHeaderRowInGridview()函数放入另一个void中。你知道吗,因为BindHeaderRowInGridview()在页面加载中可能不起作用?