ASP.NET - VB.NET - 通过多次按钮单击保留 GridView 动态选择的 DataSourceID

发布于 2024-08-11 06:01:13 字数 783 浏览 9 评论 0原文

我有一个应用程序,需要为 GridView 动态选择 SQLDataSource,以便我可以使用 2 个存储过程中的 1 个,具体取决于谁登录到系统。

我的问题是,我在按钮单击中使用这样的逻辑...

If Session("SiteType") = "Type1" Then
     GridView1.DataSourceID = "SqlDataSource2"
Else
     GridView1.DataSourceID = "SqlDataSource1"
End If
GridView1.DataBind()

当您单击显示带有 gridview 的面板的按钮时,就会发生这种情况。

然后,用户进行更改(基本上是调整网格的一个或多个留置权上的文本框),然后单击“保存”。然而,一旦发生这种情况,gridview 就不再知道它的 DataSourceID,所以当我尝试浏览 gridview 的行时 - 没有。

如果在单击“保存”按钮时输入相同的代码,它(当然)会清除我在表单中所做的任何数据更改。

那么,简单地说 - 如何动态选择 SqlDataSource,但只能选择一次,以便程序保持该 SqlDataSourceID 与 gridview 关联,直到循环结束?这是 ViewState 的事情吗?我不完全理解 ViewState...

请原谅我的无知 - 我假设这有点简单,但我只是没有大量的 .NET 经验。如果有更好的方法,我也有兴趣听到这个 - 也就是说,时间至关重要,所以我现在正在寻找快速解决方案(老板正在对我的脖子进行呼吸......呵呵) 。

谢谢!

I have an app where I need to dynamically choose an SQLDataSource for a GridView so I can use 1 of 2 stored procedures, depending on who is logged into the system.

My problem is that I am using logic like this, in the button click...

If Session("SiteType") = "Type1" Then
     GridView1.DataSourceID = "SqlDataSource2"
Else
     GridView1.DataSourceID = "SqlDataSource1"
End If
GridView1.DataBind()

This happens when you click the button that reveals the panel with the gridview in it.

The user then makes changes (basically adjusting a text box on one or more liens of the grid) and then clicks "save". However, the gridview no longer knows its DataSourceID once this happens, so when I try to go through the gridview's rows - there are none.

If, in the save button's click, I put the same code, it (of course) blanks out any of the data changes I made in the form.

So, simply put - how do I dynamically choose the SqlDataSource, but only one time, so that the program then keeps that SqlDataSourceID associated with the gridview until the end of the cycle? Is this a ViewState thing? I don't totally understand ViewState...

Pardon my ignorance - I'm assuming this is kind of simple, but I just don't have a ton of .NET experience. If there is a better way, I'd be interested in hearing that as well - that said, time is of the essence so I'm kind of looking for the quick fix right now (boss is breathing down my neck.. heh).

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

帅哥哥的热头脑 2024-08-18 06:01:13

您需要在每次回发时重新绑定网格,因为您在代码中运行时设置数据源。

将其添加到页面加载事件处理程序中:

If Page.IsPostback Then
    If Session("SiteType") = "Type1" Then
       GridView1.DataSourceID = "SqlDataSource2"
    Else
       GridView1.DataSourceID = "SqlDataSource1"
    End If
    GridView1.DataBind()
End If

You need to rebind the grid on each postback because you are setting the datasource at runtime in code.

Add this to the pages load event handler:

If Page.IsPostback Then
    If Session("SiteType") = "Type1" Then
       GridView1.DataSourceID = "SqlDataSource2"
    Else
       GridView1.DataSourceID = "SqlDataSource1"
    End If
    GridView1.DataBind()
End If
2024-08-18 06:01:13

我通过简单地将选项放入 cookie 中来完成此操作,当我再次需要它时,从 cookie 中将该选项拉下来。

Cookie、Session 和 ViewState 通常工作原理相同。它们是在有限时间内保留信息的简单方法。您创建一个密钥,添加一个值,然后将密钥保存回您将要使用的任何介质,无论是 cookie、会话还是视图状态。当您再次需要该值时,只需从持久介质中找到密钥,加载键/值对,然后通常通过将其放回到某种类型的变量中来访问该值。

祝你好运,希望这对一些人有帮助。

I've done this by simply putting the option into a cookie and when I need it again pulling that option down from the cookie.

Cookies, Session and ViewState all generally work the same. They are a simple way of persisting information for a limited amount of time. You create a key, you add a value and then save the key back to what ever medium you are going to use be it a cookie, session or viewstate. When you need the value again, you just find the key in from the perstent medium, load the key/value pair and then access the value by ususally putting it back into a variable of sometype.

Good luck, and hope this helps some.

微暖i 2024-08-18 06:01:13

我只需将其保存为会话变量即可。
前任。
Session("datasource") = myDataSource

然后您将 GridView 的数据源设置为:
MyGridView.datasource = 会话(“数据源”)
MyGridView.bind()

应该可以做到。

希望这有帮助'
大卫.

I would just go and and save it as a session variable.
Ex.
Session("datasource") = myDataSource

Then you set your GridView's datasource as such:
MyGridView.datasource = session("datasource")
MyGridView.bind()

That should do it.

Hope this helps'
David.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文