C#,vb.net,MVC,Jquery,javascript,jscript,vbscript,html,vb,sharepoint,COM,WPF,WCF,Wwf,Asp,Asp.net,questions & answers,

Latest in Sports

Showing posts with label GridView JavaScript. Show all posts
Showing posts with label GridView JavaScript. Show all posts

Tuesday, May 22, 2012

I have one gridview in one form and I have use template inside grid. In the template I have used the labels.
Problem is I want to bind the data which I have extracted from Sql table to that label.
Please help me here. I have to submit this today. I am helpless, tried all the options.
Please tell me how I do this
SOLUTION 1:
       
You can do it in 2 ways:
1.       In the aspx page set the Text property of the label to “Text = ‘<% Eval(<field name>) %>’ “ The <field Name> is the name of the field as returned from the database.
2.       For run time binding you can use the gridviews RowDataBound or DataBound event.
-          The second parameter of the RowDataBound event contains the row index of the current row with which the label control is found and text property can be assigned. This event fires when the data is bound to each row.
-          If you use DataBound event then you have to manually iterate through each row in the grid view and find the label control and assign the text property.
Hope this answers the question.
SOLUTION 2:

I have worked on the similar problem when I was in college! All you have to do is use FindControl() to find your label in gridview and the bind data to it. Following link will make this approch more clearer to you.
SOLUTION 3:
See the code below where in StartPoint is the column name of sql table.
Generally TemplateField has to be used when you want to get the data indirectly, when data is coming directly from database BoundField should be used.
<asp:TemplateField HeaderText="From">
<ItemTemplate>
        <asp:Label ID="lblFromLocation" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.StartPoint") %>'></asp:Label>
</ItemTemplate>
<ItemStyle Wrap="False" Font-Size="10pt" />
</asp:TemplateField>
You can also assign value dynamically in RowDataBound Event:
protected void gvManagePoolMembers_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lblReqStatus = (Label)e.Row.FindControl("lblMemberStatus");
            if (lblReqStatus.Text == "0")
            {
                lblReqStatus.Text = "Pending";
               // e.Row.Cells[4].Text = "Pending";
            }
        }
    }
This should solve your problem.
1.       I have a grid control on page and I have use templatefield tag . In Edittemplate and in Itemtemplate I have Command Button name as Edid and Cancel. When I click on those button for first time I am not getting anything. But for second click it is giving me JSScript Error as follow:
 
 
Microsoft JScript runtime error: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
               
2.       I have that same grid with autopaging property true. Here also for first click I am not getting anything. But for second click it is throwing me JSScript Error as below: GridView Fired PageIndexChangingEvent which was not handle.
I tried adding that event with no code Inside. Srill No result.
 
 
SOLUTION 1:
 
Bind the Gridview from view state after the post back occurs on the page. This will remove the error 1 listed below.


For 2nd  error bind the Gridview with  data source    as shown below in the PageIndexChanging event
protected void GridView1_ (object sender, GridViewPageEventArgs e) 
    { 
       
GridView1.PageIndex = e.NewPageIndex; 
        bindGridView();
//bindgridview will get the data source and bind it again 
    }