[英]Retrieving bound data from a Gridview
我有一個Gridview連接到顯示數據的SQL數據源,我希望通過按鈕和Eval檢索與所選行關聯的數據。
有點像,
<asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />
但我無法從代碼中調用Eval,我也不知道如何獲取DataKey。
我一直在網上搜索,但沒有找到任何好的東西。 誰能幫我? 非常感謝。
編輯:
<asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False"
DataKeyNames="ScheduleID" DataSourceID="SqlDataSource1">
<Colums>
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Date")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time" SortExpression="starttime">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("starttime")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat=server OnClientClick="RetrieveInfo" Text="Send" />
</ItemTemplate>
</asp:TemplateField>
</Colums>
</asp:GridView>
它被撇去了,但基本上就是這樣。
願這有幫助
編輯gridview html <Colums>
到</Columns>
<asp:LinkButton runat=server OnClientClick="RetrieveInfo" CommandName="Update" Text="Send" />
Gridview Html
<asp:GridView ID="GridView1" Width="50%" runat="server" AutoGenerateColumns="False"
onrowcommand="GridView1_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<ItemTemplate>
<asp:Label ID="lbldate" runat="server" Text='<%#Eval("Date")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time" SortExpression="starttime">
<ItemTemplate>
<asp:Label ID="lbltime" runat="server" Text='<%#Eval("starttime","{0:dd MMM yyyy}") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton runat="server" CommandName="sendvalue" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" OnClientClick="RetrieveInfo" Text="Send" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代碼背后:
protected void Page_Load(object sender, EventArgs e)
{
bindGridview();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "sendvalue")
{
int getrow = Convert.ToInt32(e.CommandArgument);
Label lbldate = (Label)GridView1.Rows[getrow].FindControl("lbldate");
Label lbltime = (Label)GridView1.Rows[getrow].FindControl("lbltime");
string getDate = lbldate.Text;
string getStartTime = lbltime.Text;
//here you retrieve all the value of select row and do your logic for link butn
GridView1.EditIndex = -1;
bindGridview();
}
}
public void bindGridview()
{
SqlDataAdapter dap = new SqlDataAdapter("select Date,startTime from yourtable", con);
DataSet ds = new DataSet();
dap.Fill(ds);
DataTable dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();
}
從你的代碼我覺得你需要在Button OnClientClick事件的Javascript函數調用中檢索這些值。你可以在GridView的RowDataBound事件中為按鈕的onclick事件設置Javascript函數中的值,並且可以輕松編寫javascript獲得這些價值的功能......
代碼隱藏: -
void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label Label1 = (Label)e.Row.FindControl("Label1Id");
Label Label2= (Label)e.Row.FindControl("Label2Id");
Label Label3= (Label)e.Row.FindControl("Label3Id");
LinkButton lnkBtn= (LinkButton)e.Row.FindControl("lnkId");
lnkBtn.Attributes.Add("onclick","RetrieveInfo('"+Label1.Text+"','"+Label2.Text+"','"+Label3.Text+"')");
}
}
使用Javascript:
<script>
function RetrieveInfo(Label1Value,Label2Value,Label3Value)
{
Write your Logic..
}
</script>
如下更改網格視圖。 添加行數據庫和行命令功能。 單擊鏈接按鈕時將調用行命令功能。
Default.aspx的
<asp:GridView ID="GridView1" Width="100%" runat="server" AutoGenerateColumns="False"
DataKeyNames="ScheduleID" DataSourceID="SqlDataSource1"
OnRowDataBound = "GridView1_OnRowDataBound"
onrowcommand="GridView1_RowCommand" >
<Colums>
<asp:TemplateField HeaderText="Date" SortExpression="Date">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("Date")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Time" SortExpression="starttime">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("starttime")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbBind" runat=server OnClientClick="RetrieveInfo" Text="Send" />
</ItemTemplate>
</asp:TemplateField>
</Colums>
</asp:GridView>
.cs背后的代碼:
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e )
{
try
{ if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drEachRow = (DataRowView)e.Row.DataItem;
// Get all controls present in the row.
LinkButton lbBind= (LinkButton )e.Row.FindControl("lbBind");
// Add row index as command argument.
lbBind.CommandArgument = e.Row.RowIndex.ToString();
}
}
catch(Exception objExp)
{
// handle exception
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
try
{ // Check if the command is generated by a button only.
if (e.CommandSource.GetType().Equals(new LinkButton().GetType()))
{
int index; // Store the index of the current row.
// Get it from CommandArgument
int.TryParse(Convert.ToString(e.CommandArgument), out index);
// One exacmple of how to get one control in the same row.
Label lblpid = (Label)gvFinalReview.Rows[index].FindControl("lblplanid");
// You can get other controls similarly.
}
}
catch(Exception objExp)
{
// handle exception
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.