如何在aspx页面加载上获取文本框值

时间:2015-10-16 12:35:31

标签: javascript jquery asp.net

伙计我有两页 GridviewWithJquery.aspx Ekle.aspx

在我点击 Ekle 按钮的第一个文件中,我将 Ekle.aspx 加载到div中(使用id="content")。

之后,我尝试将文本框值设在Ekle.aspx

我无法使用正常语法( $("#ekle_gonder").click(function){...}; )获取该值,并且它没有输入点击功能,也没有获取文本框值。

我怎样才能获得这些价值?

以下是我的代码:

GridviewWithJquery.aspx:

<script>
    $(document).ready(function () {

        var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
        var name = "";
        printValues();

        function printValues() {
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "GridviewWithJquery.aspx/fnc",
                data: "{}",
                dataType: "json",
                success: function (result) {
                    $("#gw1").find("td").remove();
                    for (var i = 0; i < result.d.length; i++) {
                        $("#gw1").append("<tr><td style='width:150px;'>" + result.d[i].Id + "</td><td style='width:150px;'>" + result.d[i].Name
                            + "</td><td style='width:150px;'>" + result.d[i].Surname + "</td><td style='width:150px;'>" + result.d[i].Sex
                            + "</td><td style='width:150px;'>" + result.d[i].Email + "</td><td style='width:150px;'>" + result.d[i].City
                            + "</td><td style='width:150px;'>" + result.d[i].Age + "</td></tr>");
                    }
                },
                error: function (result) {
                    alert("Unexpected result occured!");
                },
                complete: function () {

                    setTimeout(printValues,5000);
                }

            });
        }

        $(function () {
            $("#div_sil").dialog();

            $("#sil_name_error").dialog({
                autoOpen: false,
                buttons: [{
                    text: "OK",
                    click: function () { $(this).dialog("close"); }
                }]
            });
        })

        $("#ekle_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Ekle.aspx #ekle");

        });
        $("#sil_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Sil.aspx #div_sil");

        });
        $("#guncelle_button").click(function () {
            $("#content").dialog("open");
            $("#content").empty();
            $("#content").load("Guncelle.aspx #div_guncelle");

        });

        $(function () {
            $("#content").dialog({ autoOpen: false });
        });
    });

</script>

<body>
<form id="form1" runat="server">

    <div id="div_goruntule">

        <asp:GridView ID="gw1" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2"
            HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
            <Columns>
                <asp:BoundField ItemStyle-Width="150px" DataField="Id" HeaderText="Id" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Name" HeaderText="Name" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Surname" HeaderText="Surname" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Sex" HeaderText="Sex" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Email" HeaderText="Email" />
                <asp:BoundField ItemStyle-Width="150px" DataField="City" HeaderText="City" />
                <asp:BoundField ItemStyle-Width="150px" DataField="Age" HeaderText="Age" />
            </Columns>
        </asp:GridView>

    </div>

    <div id="buttons">
        <asp:Table ID="Table1" runat="server">
            <asp:TableRow>
                <asp:TableCell>
                    <input type="button" id="ekle_button" value="Ekle" />
                    <input type="button" id="sil_button" value="Sil" />
                    <input type="button" id="guncelle_button" value="Guncelle" />
                </asp:TableCell>
            </asp:TableRow>
        </asp:Table>
    </div>

    <div id="content">
    </div>

    <div id="ekle_part">
        <div id="ekle_id_error">
            <p>Please enter a valid id number.Id should include <b>only</b> numbers.</p>
        </div>
        <div id="ekle_name_error">
            <p>Please enter a valid name.Name should include <b>only</b> characters, not numbers.</p>
        </div>
        <div id="ekle_surname_error">
            <p>Please enter a valid surname.Surname should include <b>only</b> characters, not numbers.</p>
        </div>
        <div id="ekle_email_error">
            <p>Please enter a valid email address.Email format should be xxxxx@xxxxx.xxx</p>
        </div>
        <div id="ekle_city_error">
            <p>Please enter a valid city.City name should include <b>only</b> characters, not numbers</p>
        </div>
        <div id="ekle_age_error">
            <p>Please enter a valid age number.Age should be include <b>only</b> numbers.</p>
        </div>
    </div>

    <div id="sil_part">
        <div id="sil_name_error">
            <p>Name can not include numeric values.</p>
        </div>
    </div>

    <div id="guncelle_part">
        <div id="guncelle_id_error">Id contains only numbers.</div>
        <div id="guncelle_name_error">Name can not be empty and can not be include numeric values.</div>
        <div id="guncelle_surname_error">Surname can not be empty and can not include numeric values.</div>
        <div id="guncelle_email_error">Email format should be like xxxxxx@xxxx.com</div>
        <div id="guncelle_city_error">City can not be empty and can not be include numeric values.</div>
        <div id="guncelle_age_error">Age contains only numbers</div>
    </div>

</form>

以下是 ekle.js:

$(document).ready(function () {

var result = 1;
var id, name, surname, email, city, age;

$("#ekle_gonder").click(function () {

    var gender = document.getElementById('<%= ddl1.ClientID%>');
    var gender2 = gender.options[gender.selectedIndex].value;
    check();
    var obj = {};
    obj.Id = id;
    obj.Name = name;
    obj.Surname = surname;
    obj.Sex = gender2;
    obj.Email = email;
    obj.City = city;
    obj.Age = age;

    if (result == 1) {
        $.ajax({
            type: "post",
            url: "GridviewWithJquery.aspx/ekle_func",
            contentType: "application/json;charset:utf-8",
            data: JSON.stringify(obj),
            dataType: "json",
            success: onSuccess,
            error: function (xhr, status, error) {
                alert("no");
                var err = eval("(" + xhr.responseText + ")");
                alert(err.Message);
            }
        });
    }

});



function check() {

    var mailregex = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
    var textregex = /^[A-Za-z çğıöşü]{1,25}$/;
    var numregex = /(^\d+$)/;

    id = $("#txt1").val();
    name = $("#txt2").val();
    surname = $("#txt3").val();
    email = $("#txt5").val();
    city = $("#txt6").val();
    age = $("#txt7").val();

    if ((numregex.test(parseInt(id)) == false) || (parseInt(id) < 0)) {
        open_error_box("#ekle_id_error", "#txt1");
    }
    if ((name == '') || (textregex.test(name) == false)) {
        open_error_box("#ekle_name_error", "#txt2");
    }
    if ((surname == '') || (textregex.test(surname) == false)) {
        open_error_box("#ekle_surname_error", "#txt3");
    }
    if ((email == '') || (mailregex.test(email) == false)) {
        open_error_box("#ekle_email_error", "#txt5");
    }
    if ((city == '') || (textregex.test(city) == false)) {
        open_error_box("#ekle_city_error", "#txt6");
    }
    if ((numregex.test(parseInt(age)) == false) || (parseInt(age) < 0) || (parseInt(age) > 100)) {
        open_error_box("#ekle_age_error", "#txt7");
    }
    return result;
}

function open_error_box(error_name, name) {
    $(error_name).dialog("open");
    $(name).val('');
    result = 0;
}

function onSuccess() {

    $("#txt1").val('');
    $("#txt2").val('');
    $("#txt3").val('');
    $("#txt4").val('');
    $("#txt5").val('');
    $("#txt6").val('');
    $("#txt7").val('');
}

$(function () {
    $("#ekle").dialog();

    $("#ekle_id_error ,#ekle_name_error, #ekle_surname_error, #ekle_email_error, #ekle_city_error, #ekle_age_error").dialog(
    {
        autoOpen: false,
        buttons: [{
            text: "OK",
            click: function () { $(this).dialog("close"); }
        }]
    });
})
});

我被困了几个小时。

请告诉我一种在GridviewWithJquery.aspx页面中获取已加载项目值的方法吗?

1 个答案:

答案 0 :(得分:1)

  

我无法使用正常语法获取值(   $( “#ekle_gonder”)单击(功能){...}。 )它没有进入   点击功能也不获取文本框值。

那是因为你引用了错误的id。

在您的HTML中,按钮有以下内容。

_

请注意,ID为<input type="button" id="ekle_button" value="Ekle" />

在你的JS上,你试图绑定ekle_button

应该是:

$("#ekle_gonder").click(function () {