What I'm trying to accomplish is when I click a button in my gridview, the values of that particular row must be displayed in textlabels lblID, lblName, ...
I just don't have a clue how to do it.
This is the code of my sqldatasource:
<asp:SqlDataSource ID="srcProd" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnDB %>"
ProviderName="<%$ ConnectionStrings:ConnDB.ProviderName %>"
SelectCommand="SELECT p.tProdID, p.tProdImage, p.tProdBeschrijving, m.tMerkNaam, m.tMerkImage, p.tProdVerkoop FROM tblProducten AS p INNER JOIN tblMerken AS m ON p.tMerkID = m.tMerkID ORDER BY m.tMerkNaam">
</asp:SqlDataSource>
This is a screenshot of the gridview I'm talking about.
When I press one of the buttons I would like e.g. the parameter ID displayed in lblID.
All help, hints and tips are appreciated.
Assign a CommandName (which is like "ShowDetails" or so) and a CommandArgument (which is the ID of the record) to the button. A click on the button will now trigger the grid's RowCommand event, passing you both CommandName and CommandArgument in e. Then fetch the record from your DB and populate your controls:
<asp:TemplateField>
<ItemTemplate>
<asp:Button runat="server" Text="Foo2" CommandArgument='<%# Eval("RowID") %>'
CommandName="ShowDetails" />
</ItemTemplate>
</asp:TemplateField>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Debug.WriteLine("Command Handler");
}
SO basicly this can be done on two ways:
Add a server-side click handler behind the button and update the label inside an UpdatePanel (this case we do not need a postback)
Add some javascript on the button's "OnClientClick" event to set this label... This method can be set in the GridView's bind method.
Hope this helps.
Related
I have been trying to write this simple logic of deleting a row from the gridview and from the SQL database of the selected row, but keep getting a null reference to cell, which has the primary key [ID] in a field.
Here's my html:
<asp:GridView ID="grvInventoryEdit" runat="server" BackColor="White" onrowdeleting="grvInventoryEdit_RowDeleting"
onrowediting="grvInventoryEdit_RowEditing"
onrowupdating="grvInventoryEdit_RowUpdating">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" /><asp:TemplateField HeaderText="Id">
<ItemTemplate>
<%#Eval("No")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>........ </Columns> </asp:GridView>
And my back-end code for rowdeleting event is :
protected void grvInventoryEdit_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
TextBox id = (TextBox)grvInventoryEdit.Rows[e.RowIndex].FindControl("txtEditNo");
Asset asset = db.Assets.Single(a => a.No == Convert.ToInt32(id));
db.Assets.DeleteOnSubmit(asset);
db.SubmitChanges();
binddata();
}
and when the event fires, this is what i am seeing while debugging:
I am not sure why i am getting a null value ,though, there is a value for that cell.
Could you tell me what i am doing wrong ?
Regards,
Might be it is due to the readonly property of textbox, not suer.
If you want to use the image button for edit and delete then use
protected void ibtnDelete_Click(object sender, ImageClickEventArgs e)
{
GridViewRow gvRow = (GridViewRow)((ImageButton)sender).NamingContainer;
Int32 UserId = Convert.ToInt32(gvUsers.DataKeys[gvRow.RowIndex].Value);
// delete and hide the row from grid view
if (DeleteUserByID(UserId))
gvRow.Visible = false;
}
For complete code see
You are passing TextBox object instead instead of Text property of TextBox
Asset asset = db.Assets.Single(x=>x.No == Convert.ToInt32(id.Text));
and your TextBox is also coming null means it's unable to find it in GridView, try like this:
TextBox id = e.Row.FindControl("txtEditNo");
Also see this CodeProject article to understand how to use ItemTemplate and EditItemTemplate
why are you adding a second commandfield instead of just enabling the delete button on the existing one.
if you are using a command field you should be supplying an compatible datasource that provides Delete functionality
if you're "rolling your own" delete functionality then just use a regular Button control and supply a CommandName and CommandArgument, such as CommandName="MyDelete" CommandArgument=<row number> where <row number> is supplied via GridView RowDataBound() event.
Regardless of how you choose to implement Delete you should be placing the key field in the GridView DataKeys Property and not as a field within each row. This will make obtaining the PK far easier than what you are trying to do
I guess, in my HTML, i have only the value that's bound to the item, is being displayed, and there are no textboxes in my <ItemTemplate>, hence, it pulls null value. Whereas, in my <EditItemTemplate> there is a textbox, which can pull the value from the cell.
So I changed my HTML to this:
<asp:TemplateField HeaderText="Id">
<ItemTemplate>`<asp:label runat="server" ID="lblEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtEditNo" ReadOnly="True" Text='<%#Eval("No")%>'></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
and no change in my codebehind, and this resolved the issue.
I have a gridview that has the columns as below. These columns have textboxes with autopostback = true and need to get their value in RowDataBound of gridview. The problem is that when you type something in the textbox and take the focus off of it, occurs in the RowDataBound grid but the value entered is NOT captured (= /)
Help me to solve this problem, there is more to do. I can not use jquery or anything, only the TextChanged some textbox gridview to get the value.
Code:
<Columns>
<asp:TemplateField HeaderText="Entrada">
<ItemTemplate>
<asp:TextBox ID="txtEmanha_g" AutoPostback="true" class="Mask"
Width="40px" runat="server"
Text='<%#Eval("ENTRADA") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
You need to set your GridView up to be editable and avail of the EditItemTemplate. Here's a working example:
Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List
Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
// Bind grid here only on page load not every post back to the server
}
}
I have a control image in a user control. And I'm using a repeater to create several images list.
I have a button outside from the user control to delete image (per images).
I want to know, how can I make references to an specific row when I click in the button to delete the image.
This is the repetear:
<asp:Repeater ID="ImageRepeater" runat="server"
onitemcommand="ImageRepeater_ItemCommand">
<ItemTemplate>
<div>
<uc1:IVT_DisplayImage ID="IVT_DisplayImage1" runat="server" ImageURL="<%# Container.DataItem %>" />
<asp:Button ID="RemoveDiplayImage" Text="Remove" runat="server"
CommandName="delete"
/>
</div>
</ItemTemplate>
</asp:Repeater>
This is the event ItemCommand:
protected void ImageRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//????
}
}
I want to know, how identify by code wich row (user control) I'm selecting.
You need to add CommandArgument to your button like CommandArgument="<%# Container.ItemIndex %>".
This way, in the code; e.CommandArgument gives you the item number, with which you can get the item that you want.
Note: In fact instead of specifying ItemIndex as CommandArgument, you can use any Primary Key (Unique Identifier) from the DataSource that you bind to the repeater from where you will be able to get hold of the record directly from the list object (DataSource).
using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.
I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}