将ds.readxml stmt最小化 [英] Minimize ds.readxml stmt at aone place

查看:60
本文介绍了将ds.readxml stmt最小化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("emp.xml"));
        grdEmployee.DataSource = ds.Tables["Employee"];
        grdEmployee.DataBind();
    }

    protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
            Label lblid = (Label)e.Row.FindControl("lbldept");
            DataSet dsr = new DataSet();
            dsr.ReadXml(Server.MapPath("Dept.xml"));
            ddlDepartment.DataSource = dsr;
            
            //ddlDepartment.DataTextField = "Dname";
            //ddlDepartment.DataValueField = "Deptid";
            ddlDepartment.DataBind();

            string id = lblid.Text;
            ddlDepartment.Items.FindByValue(id).Selected = true;
            ddlDepartment.Items.Insert(0, "-Select Department-");

           // ddlDepartment.SelectedValue = e.Row.Cells[0].Text;
            
            
              
        }   
    }
}

aspx页面:

aspx page:

<asp:GridView ID="grdEmployee" runat="server" AutoGenerateColumns="false"

            onrowdatabound="grdEmployee_RowDataBound" Height="273px" Width="455px">
            <Columns>
            <asp:TemplateField>
                    <ItemTemplate>
                       <asp:Label ID="lbldept" runat="server" Text='<%#Bind("deptid") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlDepartment" DataValueField="value" DataTextField="name" runat="server">
                        </asp:DropDownList>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="name" HeaderText="name" />
                <asp:BoundField DataField="sal" HeaderText="sal" />
                <asp:BoundField DataField="Deptid" HeaderText="Deptid" ControlStyle-Width="0px" />
            </Columns>
        </asp:GridView>


在后面的代码中,我写了两次ds.readxml,因为一个emp.xml绑定了网格,而另一个则绑定了drop downlist.我如何在一个地方放置两个xml文件.dept.xml与drop downlist绑定了在gridview内.我的作业被写入以放置在一个地方,因为无需两次写入ds.readxml.如何完成?请任何人帮助我


in code behind i have written two times ds.readxml beacuse one emp.xml is binding grid and another one is binding with drop downlist.how can i place at one place i have two xml files.dept.xml is binding with drop downlist which is inside a gridview.MY assignment is write to place one place it because no need to write two times ds.readxml.How will be done ?pls anyone help me

推荐答案

这是微不足道的.这将需要两秒钟.使数据集成为成员变量并仅在页面加载中读取它.
This is trivial. It will take two seconds. Make the dataset a member variable and read it in the page load only.


您不必为每一行都加载dsr DataSet.
您可以尝试使用此代码-

You do not have to load the dsr DataSet for every row.
Can you try this code -

DataSet ds = new DataSet();
DataSet dsr = new DataSet();
protected void Page_Load(object sender, EventArgs e)
    {
        ds.ReadXml(Server.MapPath("emp.xml"));
        dsr.ReadXml(Server.MapPath("Dept.xml"));

        grdEmployee.DataSource = ds.Tables["Employee"];
        grdEmployee.DataBind();
    }
 
    protected void grdEmployee_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlDepartment = (DropDownList)e.Row.FindControl("ddlDepartment");
            Label lblid = (Label)e.Row.FindControl("lbldept");
            ddlDepartment.DataSource = dsr;
            
            //ddlDepartment.DataTextField = "Dname";
            //ddlDepartment.DataValueField = "Deptid";
            ddlDepartment.DataBind();
 
            string id = lblid.Text;
            ddlDepartment.Items.FindByValue(id).Selected = true;
            ddlDepartment.Items.Insert(0, "-Select Department-");
 
           // ddlDepartment.SelectedValue = e.Row.Cells[0].Text;
        }   
    }
}



如果愿意,请对解决方案进行投票.



Please vote the solution if you like it.


这篇关于将ds.readxml stmt最小化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆