c# - All GridView rows disappear while selecting a row -
please consider values in comments got in debug mode:
protected void filesgrid_selectedindexchanged(object sender, eventargs e) { int selected = filesgrid.selectedindex; // selected = 2 filesgrid.databind(); //added after feedback in comments. makes no change int count = filesgrid.rows.count; // count = 0 gridviewrow row = filesgrid.rows[selected]; // row = null gridviewrow row0 = filesgrid.rows[0]; // row = null }
i came code while investigating why selectedvalue gives null in event handler (the datakeynames parameter set sure).
can explain how possible?
thank in advance.
ps. here aspx code:
<asp:gridview id="filesgrid" runat="server" autogeneratecolumns="false" autogenerateselectbutton="true" onselectedindexchanged="filesgrid_selectedindexchanged" style="margin-top: 0px" > <columns> <asp:commandfield showdeletebutton="true" /> <asp:boundfield datafield="name" headertext="name" /> <asp:boundfield datafield="length" dataformatstring="{0:n0}" headertext="size in bytes" htmlencode="false" /> </columns> </asp:gridview>
here how bind data:
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { string [] dd = {"fullname"}; filesgrid.datakeynames = dd; string apppath = request.physicalapplicationpath; directoryinfo dirinfo = new directoryinfo(apppath); fileinfo[] files = dirinfo.getfiles(); filesgrid.datasource = files; filesgrid.databind(); } }
y copy pasted code, delete line filesgrid.databind() in filesgrid_selectedindexchanged, see 2 methods not posting, aren't in code posted onselectedindexchanging, onrowdeleting events, comment them aspx , see if works, or se if events not doing tricking, deleting rows in gridview.
tell me if works
i did this
protected void page_load(object sender, eventargs e) { if (!page.ispostback) { string[] dd = { "fullname" }; filesgrid.datakeynames = dd; string apppath = request.physicalapplicationpath; directoryinfo dirinfo = new directoryinfo(apppath); fileinfo[] files = dirinfo.getfiles(); filesgrid.datasource = files; filesgrid.databind(); } } protected void filesgrid_selectedindexchanged(object sender, eventargs e) { int selected = filesgrid.selectedindex; // selected = 2 //filesgrid.databind(); //added after feedback in comments. makes no change int count = filesgrid.rows.count; // count = 0 gridviewrow row = filesgrid.rows[selected]; // row = null gridviewrow row0 = filesgrid.rows[0]; // row = null } protected void filesgrid_rowdeleting(object sender, gridviewdeleteeventargs e) { } protected void filesgrid_selectedindexchanging(object sender, gridviewselecteventargs e) { }
the aspx code.
<asp:scriptmanager id="scriptmanager1" runat="server" enablescriptglobalization="true" asyncpostbacktimeout="0" enablescriptlocalization="true"> </asp:scriptmanager> <asp:updatepanel id="uppanel" runat="server"> <contenttemplate> <asp:gridview id="filesgrid" runat="server" autogeneratecolumns="false" autogenerateselectbutton="true" onrowdeleting="filesgrid_rowdeleting" onselectedindexchanged="filesgrid_selectedindexchanged" style="margin-top: 0px" onselectedindexchanging="filesgrid_selectedindexchanging"> <columns> <asp:commandfield showdeletebutton="true" /> <asp:boundfield datafield="name" headertext="name" /> <asp:boundfield datafield="length" dataformatstring="{0:n0}" headertext="size in bytes" htmlencode="false" /> </columns> </asp:gridview> </contenttemplate> </asp:updatepanel>
Comments
Post a Comment