Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在会话中存储收视率?_C#_Session_Webforms_Irepository - Fatal编程技术网

C# 如何在会话中存储收视率?

C# 如何在会话中存储收视率?,c#,session,webforms,irepository,C#,Session,Webforms,Irepository,这是一个混合(Web表单和MVC).NET的医生评级网站。下面是我的GridView。用户可以通过单击单选按钮进行评级,评级应保留在医生对象中,并且医生对象应能够显示当前用户的评级和平均用户的评级。如何在会话中存储对象?没有登录 <asp:ObjectDataSource ID="ObjectDataSourceDoctor" runat="server" DeleteMethod="Remove" InsertMethod="Add" SelectMethod="GetDoctor" T

这是一个混合(Web表单和MVC).NET的医生评级网站。下面是我的GridView。用户可以通过单击单选按钮进行评级,评级应保留在医生对象中,并且医生对象应能够显示当前用户的评级和平均用户的评级。如何在会话中存储对象?没有登录

<asp:ObjectDataSource ID="ObjectDataSourceDoctor" runat="server" DeleteMethod="Remove" InsertMethod="Add" SelectMethod="GetDoctor" TypeName="MidtermApplication.Models.TestApplicationDoctorRepo" UpdateMethod="Update" DataObjectTypeName="MidtermApplication.Models.Doctor">
    </asp:ObjectDataSource>
    <asp:GridView ID="GridViewDoctor" runat="server" DataSourceID="ObjectDataSourceDoctor" AutoGenerateColumns="False" OnSelectedIndexChanged="GridViewDoctor_SelectedIndexChanged">
        <Columns>
            <asp:ImageField DataImageUrlField="DoctorPicture" HeaderText="DoctorPicture">
            </asp:ImageField>            
            <asp:BoundField DataField="DoctorName" HeaderText="DoctorName" SortExpression="DoctorName" />
            <asp:BoundField DataField="DoctorSpecialty" HeaderText="DoctorSpecialty" SortExpression="DoctorSpecialty" />
            <asp:TemplateField HeaderText="Rate Now">
                <ItemTemplate>
                <asp:RadioButton ID="RadioButton1" runat="server" GroupName="RateNow" Text="1"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton2" runat="server" GroupName="RateNow" Text="2"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton3" runat="server" GroupName="RateNow" Text="3"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton4" runat="server" GroupName="RateNow" Text="4"></asp:RadioButton>
                <asp:RadioButton ID="RadioButton5" runat="server" GroupName="RateNow" Text="5"></asp:RadioButton>

            </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField AccessibleHeaderText="Save Rating" HeaderText="Save Rating" Text="Save" ButtonType="Button" />
            <asp:CheckBoxField DataField="fave" HeaderText="Favorite" SortExpression="fave" InsertVisible="False" />
        </Columns>
    </asp:GridView>

代码隐藏:

public partial class Rating : System.Web.UI.Page
    {
        TestApplicationDoctorRepo repo;
        Doctor doctors;
        protected List<Doctor> context;
        RateableDoctor DoctorRating = new RateableDoctor();

        protected void Page_Load(object sender, EventArgs e)
        {
            TestApplicationDoctorRepo.InitApp(this);
        }

        protected override void OnLoad(EventArgs e)
        {
            context = (List<Doctor>)Application["doctors"];

            if (context == null)
            {
                context = new TestDoctorRepository().GetDoctor();

                Application.Lock();
                Application["doctors"] = context;
                Application.UnLock();
            }

            base.OnLoad(e);
        }

        protected void SaveRepo()
        {
            Application.Lock();
            Application["doctors"] = context;
            Application.UnLock();
        }
    }
}
公共部分类评级:System.Web.UI.Page
{
测试申请十月回购;
医生;
受保护列表上下文;
应课差饷医生收费=新的应课差饷医生();
受保护的无效页面加载(对象发送方、事件参数e)
{
TestApplicationDoctorRepo.InitApp(本);
}
受保护的覆盖无效加载(事件参数e)
{
上下文=(列表)应用程序[“医生”];
if(上下文==null)
{
context=newtestdoctorrepository().GetDoctor();
Application.Lock();
应用程序[“医生”]=上下文;
Application.UnLock();
}
基础荷载(e);
}
受保护的void SaveRepo()
{
Application.Lock();
应用程序[“医生”]=上下文;
Application.UnLock();
}
}
}
第1步。应该有一个“提交”按钮

第二步。在下面创建一个onSubmitrating…伪代码:

private void OnSubmitRatingHandler(...)
{
    IDictionary<int, int> Ratings = new Dictionary<int,int>();
    for(int i = 0; GridViewDoctor.Rows.Count; i++)
    {
       Label lblDocId = GridViewDoctor.Rows[i].FindControl("lblDoctorId") as Label;
       sTmp = lblDocId.Text;
       DocId = Int32.Parse(sTmp);

       RadioButton rdr1 = GridViewDoctor.Rows[i].FindControl("RadioButton1") as RadioButton;
       ... same for rdr2/3/4/5
       if(rdr1.Checked) { Rating=1; } else if(rdr2.Checked) { Rating=2; } ...
       Ratings.Add(DocId, Rating}
    }
    return;
}
SubmitratingHandler(…)上的私有void { IDictionary Ratings=新词典(); 对于(int i=0;GridViewDoctor.Rows.Count;i++) { 标签lblDocId=GridViewDoctor.Rows[i].FindControl(“lblDoctorId”)作为标签; sTmp=lblDocId.Text; DocId=Int32.Parse(sTmp); RadioButton rdr1=GridViewDoctor.Rows[i].FindControl(“RadioButton1”)作为RadioButton; …与rdr2/3/4/5相同 如果(rdr1.Checked){Rating=1;}如果(rdr2.Checked){Rating=2;}。。。 添加(DocId,Rating} } 返回; } 步骤3.我不知道为什么您希望在会话中保存评级,而不是在某个磁盘文件或数据库中保存评级?但如果这是您想要的,那么:

增加(“医生培训”,评级)

但是,如果您想持久化信息,请尝试向会话添加一个类,该类包括用户IP地址(因为没有登录)+会话中的评级或应用程序变量,例如下面的“UserRating”

公共类用户评级
{
公共字符串UserIP=null;
公共词典评级=空;
}
在Submitratinghandler(…)上的私有无效
{
…其他东西。。。
字符串clientIp=(Request.ServerVariables[“HTTP\u X\u FORWARDED\u FOR”]??Request.ServerVariables[“REMOTE\u ADDR”]).Split(“,”)[0].Trim();
UserRating UserRating=新的UserRating();
UserRating.UserIP=clientIP;
用户评级。评级=评级;
添加(“用户评级”,用户评级);
}

我想将评分存储在会话中,因为用户可以对他们的收藏夹进行评分,并且收藏夹的医生需要存储在会话中,所以我认为评分应该存储在会话中。是的,我可以看到用户要在GridView中输入每个医生的评分-但是在用户在表单中输入评分后,用户可能会bly需要点击submit,然后评分将保存在服务器端右侧?例如,要计算提交次数内每个医生的平均评分?用户将在选择单选按钮后按“保存评分”按钮。
public class UserRating
{
   public string UserIP = null;
   public IDictionary<int, int> Ratings = null;
}

private void OnSubmitRatingHandler(...)
{
... other stuff...
string clientIp = (Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"]).Split(',')[0].Trim();
UserRating UserRating = new UserRating();
UserRating.UserIP = clientIP;
UserRating.Ratings = Ratings;
Session.Add("UserRatings", UserRating);
}