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.
我有一个应用程序,我需要为GridView动态选择一个SQLDataSource,所以我可以使用2个存储过程中的一个,具体取决于谁登录到系统。
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.
当您单击显示其中包含gridview的面板的按钮时,会发生这种情况。
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.
然后,用户进行更改(基本上调整网格的一个或多个留置上的文本框),然后单击“保存”。但是,一旦发生这种情况,gridview就不再知道它的DataSourceID,所以当我尝试浏览gridview的行时 - 没有。
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...
所以,简单地说 - 如何动态选择SqlDataSource,但只有一次,以便程序然后保持与gridview关联的SqlDataSourceID直到循环结束?这是一个ViewState的东西吗?我不完全了解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).
请原谅我的无知 - 我假设这很简单,但我没有大量的.NET经验。如果有一个更好的方法,我也有兴趣听到这一点 - 说,时间是至关重要的所以我现在正在寻找快速解决方案(老板正在我的脖子上呼吸......嘿) 。
Thanks!
谢谢!
3 个解决方案
#1
1
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
#2
0
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.
我通过简单地将选项放入cookie中来完成此操作,当我需要它再次从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.
Cookies,Session和ViewState通常都是一样的。它们是在有限的时间内保持信息的简单方法。您创建一个密钥,添加一个值,然后将密钥保存回您将要使用的媒体,无论是cookie,会话还是视图状态。当您再次需要该值时,只需从perstent介质中找到键,加载键/值对,然后通过将其放回某个类型的变量中来访问该值。
Good luck, and hope this helps some.
祝你好运,希望这对一些人有所帮助。
#3
0
I would just go and and save it as a session variable. Ex. Session("datasource") = myDataSource
我会去并将其保存为会话变量。防爆。会话(“datasource”)= myDataSource
Then you set your GridView's datasource as such: MyGridView.datasource = session("datasource") MyGridView.bind()
然后你设置你的GridView的数据源:MyGridView.datasource = session(“datasource”)MyGridView.bind()
That should do it.
应该这样做。
Hope this helps' David.
希望这有助于'大卫。
#1
1
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
#2
0
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.
我通过简单地将选项放入cookie中来完成此操作,当我需要它再次从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.
Cookies,Session和ViewState通常都是一样的。它们是在有限的时间内保持信息的简单方法。您创建一个密钥,添加一个值,然后将密钥保存回您将要使用的媒体,无论是cookie,会话还是视图状态。当您再次需要该值时,只需从perstent介质中找到键,加载键/值对,然后通过将其放回某个类型的变量中来访问该值。
Good luck, and hope this helps some.
祝你好运,希望这对一些人有所帮助。
#3
0
I would just go and and save it as a session variable. Ex. Session("datasource") = myDataSource
我会去并将其保存为会话变量。防爆。会话(“datasource”)= myDataSource
Then you set your GridView's datasource as such: MyGridView.datasource = session("datasource") MyGridView.bind()
然后你设置你的GridView的数据源:MyGridView.datasource = session(“datasource”)MyGridView.bind()
That should do it.
应该这样做。
Hope this helps' David.
希望这有助于'大卫。