user1263981
user1263981

Reputation: 3147

Should i make a call to DB or save value in viewstate

On asp.net page, I am using tabs and one of the tab has got user control on it.

On the first tab, data is being displayed from table A and the second tab (which has user control on it) is getting data from table B.

On the user control (second Tab), I need to show the column value of table A. It is just one string value.

I am wondering if there is any best way of displaying the value of a table A column without making a call to database?

The way code has been designed, I can’t access the user control’s textbox from the first tab.

I can only think of using view state or session but don’t know if I should use them instead of making call to DB.

I want value to live for the current page's lifecycle.

Upvotes: 1

Views: 142

Answers (3)

Gonzix
Gonzix

Reputation: 1206

If you have a complex form and need to split into smaller chunks I would use a multiview control with as many views as you need to complete your task. If you design each view with its own controls, validation groups and logics .net will do the rest, you won't have to manually deal with states or saving middle steps

<asp:MultiView ID="MultiView1" runat="server">
    <asp:View ID="View1" runat="server">
        <asp:TextBox ID="txt1" runat="server" />
        <asp:Button ID="Button1" runat="server" Text="Next" OnClick="NextStep_Click" />
    </asp:View>
<asp:View ID="View2" runat="server">
    <asp:TextBox ID="txt2" runat="server" />
    <asp:Button ID="Button2" runat="server" Text="End" OnClick="EndProcess_Click" />
</asp:View>  
</asp:MultiView>
<asp:TextBox ID="txt3" runat="server" />

In code behind

protected void NextStep_Click(object sender, EventArgs e)
{
    MultiView1.SetActiveView(View2);
    txt2.Text = txt1.Text;
}

protected void EndProcess_Click(object sender, EventArgs e)
{
    txt3.Text = txt1.Text + " " + txt2.Text;
}

you can go back and forth the times you want and won't have to worry with the values the users entered. Obviously, you have to put buttons to go back and just set the active view you want.

Upvotes: 0

smiling4ever
smiling4ever

Reputation: 152

How about using js to copy the data contents from tab1? Are you loading the usercontrols in tabs using ajax?

Upvotes: 0

BlackjacketMack
BlackjacketMack

Reputation: 5692

If you can save it in viewstate then go for it. But there are plenty of storage options in addition to just viewstate:

  • querystring (good for Ids, not great for strings)
  • cookie (pretty straight forward)
  • local storage (HTML 5 only)
  • cache (you could still appear to make the call but just have the results cached. you then have to deal with cache expiration as well)
  • session (as you mentioned, this is basically a per-person cache usage, but is not a bad option)
  • hidden field (basically what viewstate is)

Even with all of those options, the viewstate is going to be a pretty good one, but it just requires that you post back the viewstate every time you need that value.

Upvotes: 2

Related Questions