3

我正在使用一个网格视图,我希望能够单击每一行以显示我正在显示的对象中的另一个字段。感觉很容易解决,但也许我很愚蠢,因为我在任何地方都找不到它......

ASP代码:

<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false" 
    CaptionAlign="NotSet" CellPadding="5">
    <Columns>
        <asp:BoundField HeaderText="Avsändare" DataField="Sender" />
        <asp:BoundField HeaderText="Ämne" DataField="Head" />
    </Columns>
</asp:GridView>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
        gvMessages.DataSource = con.GetMails(con.GetId(Membership.GetUser().UserName));
        gvMessages.DataBind();
    }

不确定所有这些对于问题来说都是必要的,但这是我的 wcf 项目中的方法,它用对象信息填充我的复合类

public List<MailInfo> GetMails(int id)
    {
        using (var client = new datingEntities())
        {
            var result = client.Mail.Where(x => x.SentTo == id).Select(x => new MailInfo
            {
                Message = x.Mail1,
                Reciever = x.SentTo,
                Read = (bool)x.IsRead,
                Sender = (int)x.SentFrom,
                Head = x.Subject
            }).ToList();
            return result;
        }
    }

复合类:

[DataContract]
public class MailInfo : Mail
{
    [DataMember]
    public string Message { get; set; }
    [DataMember]
    public int Reciever { get; set; }
    [DataMember]
    public bool Read { get; set; }
    [DataMember]
    public int Sender { get; set; }
    [DataMember]
    public string Head { get; set; }
}
4

2 回答 2

4

您应该数据绑定GridView唯一的if(!Page.IsPostBack).

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostBack)
    {
        gvMessages.DataSource = con.GetMails(con.GetId(Membership.GetUser().UserName));
        gvMessages.DataBind();
    }
}

如果您想在点击时选择一行,您可以使用 javascript:

protected void gvMessages_RowCreated(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow) {
        e.Row.Attributes["onmouseover"] = "this.style.cursor='pointer';this.style.textDecoration='underline';";
        e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
        e.Row.ToolTip = "Click to select row";
        e.Row.Attributes["onclick"] = this.Page.ClientScript.GetPostBackClientHyperlink(this.GridView1, "Select$" + e.Row.RowIndex);
    }
}

现在,您可以SelectedIndexChangedEvent在用户单击行中的某处时处理:

protected void gvMessages_SelectedIndexChanged(Object sender, EventArgs e)
{
    // Get the currently selected row using the SelectedRow property.
    GridViewRow row = CustomersGridView.SelectedRow;
}
于 2013-01-08T20:55:15.573 回答
1

您应该使用该OnSelectedIndexChanged事件。

<asp:GridView ID="gvMessages" runat="server" AutoGenerateColumns = "false" 
    OnSelectedIndexChanged="gvMessages_SelectedIndexChanged"  
    CaptionAlign="NotSet" CellPadding="5">
    <Columns>
        <asp:BoundField HeaderText="Avsändare" DataField="Sender" />
        <asp:BoundField HeaderText="Ämne" DataField="Head" />
    </Columns>
</asp:GridView> 

然后在事件的定义中,您可以检索所选项目并从那里执行您需要的任何操作。

protected void gvMessages_SelectedIndexChanged(object sender, EventArgs e)
{
    if (ContactsGridView.SelectedIndex >= 0)
       ViewState["SelectedKey"] = gvMessages.SelectedValue;
    else
       ViewState["SelectedKey"] = null;
}

示例来自MSDN 官方文档

于 2013-01-08T20:56:05.137 回答