简体   繁体   English

ASP中继器和数据绑定

[英]asp repeater and databind

I'm having a problem with my asp-repeaters. 我的asp中继器有问题。

I have a link in a gridview, that opens a popup that contains additional information. 我在gridview中有一个链接,该链接打开一个包含其他信息的弹出窗口。

<asp:TemplateField HeaderText="Firma Info">
    <ItemTemplate>

        <a href="#" onclick='openWindow("<%# Eval("CompanyID") %>");'>Vis detaljer</a>
    </ItemTemplate>
</asp:TemplateField>  

Here is the javascript: 这是JavaScript:

<script type="text/javascript">
    function openWindow(CompanyID) {
        window.open('CompanyTest.aspx?id=' + CompanyID, 'open_window', ' width=640, height=480, left=0, top=0');

    }
</script>

For the repeaters on CompanyTest (the popup), I have used the code from the following site: http://atul-dhiman.blogspot.dk/2011/06/aspnet-using-c-repeater-control.html because I needed the edit-functionality. 对于CompanyTest(弹出窗口)上的转发器,我使用了以下网站的代码: http : //atul-dhiman.blogspot.dk/2011/06/aspnet-using-c-repeater-control.html,因为我需要编辑功能。

Most notably, is the source to get the data: 最值得注意的是获取数据的来源:

public void Show_Data()
{
    SqlDataAdapter adp = new SqlDataAdapter("select * from Company", con);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    Repeater1.DataSource = ds;
    Repeater1.DataBind();
}

That just returns all the data I have, instead of just returning the data based on the CompanyID in the popup-window. 那只会返回我拥有的所有数据,而不是仅仅基于弹出窗口中的CompanyID返回数据。

I have tried to replacing it with 我试图用替换它

CompanyDataContext db = new CompanyDataContext();
int id = Convert.ToInt32(Request.QueryString["CompanyID"]);


Repeater1.DataSource = db.Companies.Where(x => x.CompanyID == id);
Repeater1.DataBind();

But then my repeaters doesnt show anything. 但是后来我的中继器什么也没显示。

Where does it go wrong? 哪里出错了?

As I understand your code, in your page you use the following link with a query parameter named id : 据我了解您的代码,在您的页面中,使用以下链接以及名为id的查询参数:

CompanyTest.aspx?id=123

But in your code, you use the name CompanyId : 但是在您的代码中,使用名称CompanyId

int id = Convert.ToInt32(Request.QueryString["CompanyID"]);

So I think changing your code to 所以我认为将您的代码更改为

int id = Convert.ToInt32(Request.QueryString["id"]);

should help. 应该有所帮助。


If you want to solve the problem based on the SqlDataAdapter, you can use the following code: 如果要基于SqlDataAdapter解决问题,则可以使用以下代码:

public void Show_Data()
{
    int id = Convert.ToInt32(Request.QueryString["id"]);
    using (var cmd = con.CreateCommand())
    {
        cmd.CommandText = "select * from Company where CompanyId = @id"
        cmd.Parameters.AddWithValue("@id", id);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM