문제
클릭할 때 내에서 행트,내가 가고 싶어하는 다른 페이지의 ID 나에서 얻을 데이터베이스입니다.
내 RowCreated 이벤트가 다음 줄:
e.Row.Attributes.Add(
"onClick",
ClientScript.GetPostBackClientHyperlink(
this.grdSearchResults, "Select$" + e.Row.RowIndex));
하여 오류 메시지가 발생하지 않도록 내가 이 코드:
protected override void Render(HtmlTextWriter writer)
{
// .NET will refuse to accept "unknown" postbacks for security reasons.
// Because of this we have to register all possible callbacks
// This must be done in Render, hence the override
for (int i = 0; i < grdSearchResults.Rows.Count; i++)
{
Page.ClientScript.RegisterForEventValidation(
new System.Web.UI.PostBackOptions(
grdSearchResults, "Select$" + i.ToString()));
}
// Do the standard rendering stuff
base.Render(writer);
}
할 수 있는 방법을 제공 행한 고유 ID(DB)and when I 우스 오른쪽 단추로 클릭하고,다른 페이지가 열립니다(같은 클릭 a href)및 해당 페이지를 읽을 수 있습 ID 입니다.
해결책
해결책이 있습니다.
이것이 제가 한 일입니다.
if(e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onClick"] = "location.href='view.aspx?id=" + DataBinder.Eval(e.Row.DataItem, "id") + "'";
}
나는 선행 코드를 행 다타쪽 이벤트에 넣었습니다.
다른 팁
Uml 정적 구,
여기에 또 다른 예는 몇 가지 멋진 줄을 강조하고 href 스타일로 커서:
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
e.Row.Attributes.Add("style", "cursor:pointer;");
e.Row.Attributes.Add("onclick", "location='patron_detail.aspx?id=" + e.Row.Cells[0].Text + "'");
}
}
위의 코드 작동합니다.NET3.5.그러나,설정할 수 없습니다 당신의 id 가 열려 볼="false"기 때문에 당신을 얻을 것 빈 쿼리에는 문자열 값에 대한 당신의 id 키:
<asp:GridView ID="gvSearch" runat="server" OnRowDataBound="gvSearch_RowDataBound" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="id" Visible="false" />
<asp:BoundField DataField="first_name" HeaderText="First" />
<asp:BoundField DataField="last_name" HeaderText="Last" />
<asp:BoundField DataField="email" HeaderText="Email" />
<asp:BoundField DataField="state_name" HeaderText="State" />
</Columns>
</asp:GridView>
그래서 변경 첫 번째 열 이는 대신:
<asp:BoundField DataField="id" ItemStyle-CssClass="hide" />
추가 이 css 의 상단 페이지:
<head>
<style type="text/css">
.hide{
display:none;
}
</style>
<head>
하지만 숨기기의 첫 번째 셀의 헤더 줄을 추가,이하의 gvSearch_RowDataBound()코드에서 숨김:
if (e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].CssClass = "hide";
}
분명히 있을 수 있습 숨겨진 id 열 코드에서 뒤에,너무나 이상에서 텍스트 마크업보다 css 클래스:
e.Row.Cells[0].Attributes.Add("style", "display:none;");
e.Row.Attributes.Add("style", "cursor:pointer;");
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
GridViewRow gvr = e.Row;
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
gvr.Attributes.Add("OnClick", "javascript:location.href='Default.aspx?id=" + abc + "'");
}
}
}
ID가 GridView에 표시된 데이터 항목과 관련이 있습니까?
그렇다면 e.row.dataitem을 사용하여 어떤 유형이든지 캐스트 할 수 있습니다.
그리드보기의 RowCommand 이벤트를 사용할 수 있습니다. 클릭을 설정하기 위해 WNAT 버튼/링크에서 명령 이름과 CommandArgument를 설정하여 이벤트 메소드의 EventArgs 매개 변수에서 accees 할 수 있습니다.
줄을 클릭하여 그리드보기 다른 페이지로 리디렉션됩니다
protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string abc = ((GridView)sender).DataKeys[e.Row.RowIndex].Value.ToString();
e.Row.Attributes["onClick"] = "location.href='Default.aspx?id=" + abc + "'";
}
}
절대적으로 잘 작동합니다
Johnb, 귀하의 코드는 매우 잘 작동합니다. 마우스 아웃 후 교대로 스타일이 망가지 않도록 약간의 해킹을 추가했습니다.
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
바뀌었다:
e.Row.Attributes.Add("onmouseout", "if(" + e.Row.RowIndex + "% 2 == 0) { this.style.backgroundColor=''; } else { this.style.backgroundColor = '#E8F7EA'; }");
더 좋은 방법이 있다면 알려주십시오. 그러나 그것은 나에게 완벽하게 작동합니다.
인사말.