adding button to gridview - asp.net

I want to add an Update button to my database table in my gridview. I have added a template field but i don't know how to add a button on it.
I'm using asp.net 4.0. In Update method it will change only 1 column called as kitap_yeri when button is clicked. How can that be achieved?

In your aspx file add the following:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="Button1" OnClick="Button1_Click"
AutoPostBack="true" runat="server"/>
</ItemTemplate>
<ItemStyle Width="80px" />
</asp:TemplateField>
In your .cs file add the following:
protected void Button1_Click(object sender, EventArgs e)
{
// ...Code...
}

Related

Adding buttons in DataRow in DataTable

I have dynamically created DataTable to bind the GridView. I have two buttons, their visibility is set to false. I want when I add a new row on button click I want one of the buttons to be set visibility=true in that new row, and the other one button to stay visibility=false. So the button should be visible only for the row which the user adds, not visible in all rows in DataTable. Here is my code, I don't have any idea how to fix this. Please help
Markup:
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button189" Visible="false" OnClick="Button189_Click" runat="server" Text="odzemi svez vrganj" />
<asp:Button ID="btnTest" Visible="false" runat="server" CommandName="odzemi" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind:
protected void Button5_Click(object sender, EventArgs e)
{
MethodForAddFirstRow();
//Here i need somehow to set bnTest to be visible only for this row,other button to stay invisible
}
protected void Button5_Click(object sender, EventArgs e)
{
MethodForAddSecondRow();
//Here I need somehow to set Button189 to be visible only for this row,other button to stay invisible
}
In order to handle events of buttons inside grid you need to use OnRowCommand so you need to update your grid to be like the following
<asp:GridView ID="GridView2" runat="server" OnRowDataBound="GridView2_RowDataBound" onrowcommand="gv_RowCommand">
and make sure that each button has CommandName attribute like the following
<asp:Button ID="Button189" Visible="false" OnClick="Button189_Click" runat="server" Text="odzemi svez vrganj" CommandName="Command1" />
<asp:Button ID="btnTest" Visible="false" runat="server" CommandName="Command2" CssClass="button2" OnClick="btnTest_Click" Text="-" Width="100px" Font-Bold="True" />
then create the following event handler inside the code behind
void gv_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Command1")
{
}
else if(e.CommandName=="Command2")
{
}
}
In order to access buttons inside the rowcommand event handler you can use the following
GridView customersGridView = (GridView)e.CommandSource;
GridViewRow row = customersGridView.Rows[index];
Button btn = (Button)row.FindControl("Button189");
btn.Visible=false;

Add a clickable image to a GridView in ASP.Net

I'm trying to add an delete image at the end of each row in a GridView. I want the user to be able to click the image to delete the row.
So I've used a HyperLinkField to create a link to another page which will delete the record:
<asp:HyperLinkField DataNavigateUrlFields="ID"
DataNavigateUrlFormatString="RemoveLine.aspx?ID={0}"
Target="_blank" />
The HyperLinkField doesn't contain an Image tag so I created a TemplateField with an Image inside.
<asp:TemplateField>
<ItemTemplate>
<asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif" />
</ItemTemplate>
</asp:TemplateField>
However the HyperLinkField and Image appear in different columns and the image has no click event.
Any way of combining the two?
I'm using ASP.Net 4.0.
Thanks in advance
Normally you need to identify which record you want to delete, you can use the CommadArgument property to identify the record's Id:
<asp:TemplateField HeaderStyle-Width="40">
<ItemTemplate>
<asp:ImageButton ID="ButtonDelete" runat="server"
ImageUrl="~/Imags/delete.png" OnClick="ButtonDelete_Click" ToolTip="Delete"
CommandArgument='<%#Bind("UserId")%>'/>
</ItemTemplate>
</asp:TemplateField>
protected void ButtonDelete_Click(object sender, EventArgs e)
{
ImageButton button = sender as ImageButton;
DeleteUserById(Convert.ToInt32(button.CommandArgument));
}
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server"
ImageUrl="~/Imags/delete.png" OnClick="btnDelete_Click"
ToolTip="Delete row" CommandName="Eliminar" CommandArgument='<%#Eval("UserId")%>'/>
</ItemTemplate>
You can use the CommandArgument to pass the ID value of the selected row and perform
the desires results
// fires when the ImageButton gets clicked
protected void GridView1_ItemCommand(object sender, DataGridCommandEventArgs e)
{
if(e.Commandname ="Eliminar"){
this.Eliminar(Convert.ToInt32(e.CommandArgument));
}
}
// function to delete the record
private void Eliminar(int code)
{
//custom code to delete the records
}
How about an image button?
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server"
ImageUrl="~/Images/smallcross.gif" onclick="ImageButton1_Click" />
</ItemTemplate>
you can wrap your image with a raw anchor tag , you get the same result.
<asp:TemplateField>
<ItemTemplate>
<a href="RemoveLine.aspx?ID={0}">
<asp:Image ID="imageRemove" runat="server" ImageUrl="~/Images/smallcross.gif" />
</a>
</ItemTemplate>
</asp:TemplateField>

How to access "textChanged" event from text box that added to telerik Rad Grid?

Hi There Do you know how to access textboxes textChanged event added to a radgrid that are bound but are used to trap any row related input a user typed in to the textbox for that column. I need to access this data server side when a postback occurs. Your thoughts are greatly appreciated Thanking you
Being in a RadGrid really doesn't change much. Set AutoPostBack="true" on the TextBox, and create an OnTextChanged event handler:
<telerik:RadGrid ID="RadGrid1" runat="server">
<MasterTableView AutoGenerateColumns="false">
<Columns>
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
In code-behind:
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
TextBox txt = sender as TextBox;
if (txt != null)
{
//some logic here
}
}

ASP.NET Grid View Problem

I have one gridview where I am passing the command argument as gridview row id for the Button I created for every row.
I want to display all the details of that row in the textbox according to the Row clicked.
<asp:GridView ID="gvCategory" runat="server" AutoGenerateColumns="false"
onrowcommand="gvCategory_RowCommand" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCatId" runat="server" Text='<%#Eval("categoryId") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lblCatName" runat="server" Text='<%#Eval("categoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnModify" runat="server" Text="Modify" CommandName="Modify" CommandArgument='<%#Eval("categoryId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code.....
if (e.CommandName == "Modify")
{
int id = Convert.ToInt32(e.CommandArgument);
// I want the value to assgin for the selected row here
// I was previously fetching the data from database according id,but i want this frim the gridview of selected row.
}
I wrote a quick example of how to do what you're trying to do. It works for me.
The Example Solution
Default.aspx
<asp:GridView ID="myGridView" runat="server"
AutoGenerateColumns="False"
DataSourceID="StudentsDS"
DataKeyNames="ID"
OnRowCommand="myGridView_RowCommand"
OnSelectedIndexChanged="myGridView_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="FullName" HeaderText="FullName"
SortExpression="FullName" />
<asp:BoundField DataField="ClassID" HeaderText="ClassID"
SortExpression="ClassID" />
<asp:CommandField ShowSelectButton="True" SelectText="Modify" />
</Columns>
</asp:GridView>
<asp:TextBox ID="txtStudent" runat="server" />
<asp:SqlDataSource ID="StudentsDS" runat="server"
ConnectionString="<%$ ConnectionStrings:Sandbox %>"
SelectCommand="SELECT * FROM Student"
/>
Default.aspx.cs
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e) {
if (e.CommandName == "Select") {
// do something here if you want, although not necessary
}
}
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
// show "FullName" field of selected row in textbox
txtStudent.Text = myGridView.SelectedRow.Cells[1].Text;
}
How It Works
Upon clicking "Modify" in a row, the textbox updates to show the FullName field of the selected row.
The important part is that instead of a TemplateField, use a CommandField with ShowSelectButton="True". Then do what you need to do in the SelectedIndexChanged event handler. Note that the SelectText of the CommandField is set to "Modify" as you desired. You can also set the ButtonType property of the CommandField to be button, image, or link.
Making It Better
I would also advise that instead of using a SqlDataSource as I have, you use an ObjectDataSource so that you can do something like
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e) {
MyModelObject o = myGridView.SelectedRow.DataItem as MyModelObject;
txtStudent.Text = o.MyProperty;
}
You may also consider wrapping your GridView in an UpdatePanel to prevent full postbacks / page refreshes.
After you have the lineID, you select that line, and then you have the CategoryID on selected value
int iTheIndexNow = Convert.ToInt32(e.CommandArgument);
// select that line to see it visual as selected, and get the value on next line
gvCategory.SelectedIndex = iTheIndexNow;
// here is your categoryID that
//you can use to open your database with and get the text
gvCategory.SelectedValue // == categoryID
On GridView you must have set your KeyID
<asp:GridView DataKeyNames="categoryId" .... >

GridView: Get datakey of the row on button click

How can I get a value of the DataKeyName of a GridView Row when I have a button inside a row that have an OnClick event. In my OnClick event of my button I want to get a the DataKeyName of the row where the button resides.
Is this possible?
<asp:GridView ID="myGridView" run="server">
<Columns>
<asp:TemplateField HeaderText="Column 1">
<ItemTemplate>
... bunch of html codes
<asp:Button ID="myButton" UseSubmitBehavior="false" runat="server" Text="Click Me" onclick="btnClick_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In my codebehind
protected void btnClick_Click(object sender, EventArgs e)
{
// How can I get the DataKeyName value of the Row that the Button was clicked?
}
When I'm working with a GridView, I usually do not use the OnClick event for buttons. Instead, I use the OnRowCommand event on the GridView, and bind data to the CommandArgument property of the button. You can retrieve the command argument from the GridViewCommandEventArgs parameter of the event handler.
You can use the CommandName parameter to bind an arbitrary string to distinguish between any different kinds of buttons you have. Note that some command names are already used for other events, such as "Edit", "Update", "Cancel", "Select" and "Delete".
Update:
Here is an example, assuming the data key is called "ID":
<asp:GridView ID="myGridView" runat="server" OnRowCommand="myGridView_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Column 1">
<ItemTemplate>
... bunch of html codes
<asp:Button ID="myButton" runat="server" Text="Click Me" CommandName="ClickMe" CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And in the code-behind:
protected void myGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
var datakey = e.CommandArgument;
// ...
}
You can bind the key to the CommandArgument property of the Button, and consume (sender as Button).CommandArgument property in the code

Resources