这是一个用于评级医生的混合(Web表单和MVC).NET网站。下面是我的GridView。用户可以通过单击单选按钮进行评级,评级应该保留在Doctor对象中,Doctor对象应该能够显示当前用户的评级和普通用户的评级。如何在会话中存储对象?没有登录。
<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();
}
}
}
答案 0 :(得分:0)
步骤1.应该有一个“SubmitRating”按钮
步骤2.在下面创建一个OnSubmitRatingHandling ...伪代码:
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;
}
步骤3.我不知道你为什么要在Session中保存评级而不是在某个磁盘文件或数据库中?但如果这是你想要的,那么:
Session.Add(“DoctorRatings”,评级);
但是,如果您想要保留信息,请尝试向会话添加一个类,其中包括用户IP地址(因为没有登录)+会话或应用程序变量的评级 - 例如下面的“UserRating”
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);
}