我有一个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>
它被撇去了,但基本上就是这样。
答案 0 :(得分:3)
愿这个帮助
将您的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();
}
答案 1 :(得分:0)
从您的代码中我觉得您需要在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>
答案 2 :(得分:0)
按如下方式更改网格视图。添加行数据库和行命令功能。 单击链接按钮时将调用行命令功能。
<强> 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
}
}