gpt4 book ai didi

asp.net - 从 Gridview 检索绑定(bind)数据

转载 作者:行者123 更新时间:2023-12-04 15:35:33 25 4
gpt4 key购买 nike

我有一个连接到显示数据的 SQL 数据源的 Gridview,我希望做的是通过按钮和 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();
}

关于asp.net - 从 Gridview 检索绑定(bind)数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11877162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com