I'm trying to use a button in my gridview to pass the ID which is a GUID from my selected row to my database where I compare it with the guids in the database, I then pass it to my "Show" function and get the post from the database.
Right now I have no idea how to get the object "visadabok" from my button to my "Show".
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
}
else
{
//TextBox68.Text = Request["ID"];
if (!string.IsNullOrEmpty(TextBox68.Text))
{
Show(GridView1_RowCommand());
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Valj")
{
var valj = new Guid((string)e.CommandArgument);
var visadagbok = (from x in DagbokFactoryBase.All
where (x.ID == valj)
select x).FirstOrDefault();
return visadagbok;
}
}
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="AddButton" runat="server"
CommandName="Valj"
CommandArgument="<%# ((GridViewRow) Container).ID %>"
Text="Gå till" />
</ItemTemplate>
</asp:TemplateField>
The return type for GridView1_RowCommand
is void, hence the method won't be able to return any data to the Show
method. There are 2 options which you can prefer to perform
Show
method from the GridView1_RowCommand
method (Best option as .net framework performs the event calling for the grid)e.g.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Valj")
{
var valj = new Guid((string)e.CommandArgument);
var visadagbok = (from x in DagbokFactoryBase.All
where (x.ID == valj)
select x).FirstOrDefault();
Show(visadagbok);
}
}
like:
public delegate void ShowHandler(string id);
public string Id
{
set
{
ShowHandler _show = new ShowHandler(Show);
if (_show != null)
{
_show(value);
}
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//set of existing code
Id = visadagbok
}
public void Show(string Id)
{
//set of code
}