0

aspx part :

<body>
<form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="~/Models.asmx" />
        </Services>
    </asp:ScriptManager>
    <div>
        <div>
            <br />
            <div>
                <asp:GridView ID="Gridview1" clientID="Gridview1" runat="server"></asp:GridView>
                <br />
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </div>
            &nbsp;
            <input id="Button2" type="button" value="Get Model" onclick="getModelByID()" /><br />
        </div>
    </div>
</form>

JavaScript part:

<script type="text/javascript" language="javascript">

function getModelByID() {
    _4_layer.Models.GetAllModelss(SuccesCallBack);
}
function parseJSON(data) {
    return window.JSON && window.JSON.parse ? window.JSON.parse(data) : (new Function("return " + data))();
}
function SuccesCallBack(result) {
    obj = parseJSON(result);
    var i = document.getElementById("TextBox1");     
    var j = document.getElementById("GridView1");
    j.DataSource = obj;
    j.Bind();
}

also use

("<%= Gridview1.ClientID %>")

everything is ok with i but j returns the null object in both implementation. It's WebApp. at the beginning the Gridview1 is empty so it doesn't show and no html object creates for it to use the Dom object. what can I do ? with out Jquery!

  • 1
    what about using an outer div with an id and address the inner asp:xxx element with the other div element.nextSibling or element.firstChild – Marcel Ennix Feb 01 '15 at 21:34
  • Where is the JavaScript code placed in relation to the HTML? – Felix Kling Feb 01 '15 at 21:34
  • JavaScript is in script tag in .aspx html part at first. I'm not sure how to implement outer div id to address the inner asp... – MHR Navazani Feb 01 '15 at 21:41
  • Can't really help you without more information (don't know what "JavaScript is in script tag in .aspx html part at first" means). But maybe this helps: [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/q/14028959/218196) – Felix Kling Feb 01 '15 at 21:44
  • I've edit the code to give u more information. – MHR Navazani Feb 01 '15 at 22:00
  • no it doesn't relate to "order matters" it's because of the emptiness of the gridview1 at the beginning, so Html Dom object doesn't include it at first. I don't know how to solve it. – MHR Navazani Feb 01 '15 at 22:11

2 Answers2

0

var j = document.getElementById("Gridview1");
j.DataSource = obj;

It seems that you are trying to use method and attribute that does not exist.

There is bind() in javascript but written "bind" and it is used for functions. If it's not previously definded there is no DataSource.

Btw why do you use two semicolons?

Maybe for sake of debugging split your code into:

0

I've solved the problem by initialize the Gridview1 at the page_load by a manual entry row , now how to do it without initializing the grid?