Codechef4u is a community for computer professionals,by computer professionals,just like you; who loves sharing and helping each others,Join them
Share your post

Display radio button list inline?

First we will understand what is RadioButtonList control?

The RadioButtonList control allows you to create a single-selection radio button group that can be dynamically generated by binding to a data source.
You can add item that you want to appear in the RadioButtonList control place a ListItem element for each entry between the opening and closing tags of the RadioButtonList control.

Solution:

Displaying Radio button list inline

      <asp:RadioButtonList ID="rdFileType" RepeatColumns="4"
runat="server" CssClass="radio">
                             <asp:ListItem>All</asp:ListItem>
                             <asp:ListItem>PDF</asp:ListItem>
                             <asp:ListItem>HTML</asp:ListItem>
                             <asp:ListItem>CHM</asp:ListItem>
                            </asp:RadioButtonList>

 

1.  Set RepeatColumns propery with number of items

Example: RepeatColumns="4"

2.  You can set property RepeatDirection to horizontal also instead of css class 

Example: RepeatDirection="Horizontal"

 

3.  CSS class settings

.radio label
{
    display:inline;
}

Get Multiple DataKeyname Values from Gridview RowCommand in Asp.net

In this article I will explain how to retrieve multiple data key values from asp.net gridview
On command button action using grid view RowCommand event.

GridView datakeynames multiple values

In following grid we are assigning two values product id and category Id to gridview DataKeyNames property.

I want to delete specific row data for only deactivated categories reason I want to pass product id and category id.

I am passing grid view row index using Command Argument for example.

CommandArgument ='<%# DataBinder.Eval(Container, "RowIndex") %>'

ASPX fro Grid:

<asp:GridView ID="GrdProducts" runat="server" AutoGenerateColumns="False" CellPadding="4"  DataKeyNames="ProductID,CategoryId"
OnRowCommand="GrdProducts_RowCommand" >
<Columns>
<asp:TemplateField HeaderText="Product">
<ItemTemplate>       
<asp:TextBox ID="txtProductName" runat="Server" Text='<%# Eval("ProductName") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Product Price">
<ItemTemplate>       
<asp:TextBox ID="txtProductPrice" runat="Server" Text='<%# Eval("Price") %>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete?">
<ItemTemplate>
<asp:LinkButton ID="lnkDelete" runat="Server" Text="Delete" CommandName="Delete" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
 </Columns>
</asp:GridView>

GridView datakeynames get values Example:

C# RowCommand Event:

Following code retrieves datakeynames get values using command argument row index value.

protected void GrdProducts_RowCommand(object sender, GridViewCommandEventArgs e)
      {
         GridView testGrid = (GridView)sender;
 
       if (e.CommandName.Equals("Delete"))
         {
             int rowIndex = int.Parse(e.CommandArgument.ToString());
             //Get the value of column from the DataKeys using the RowIndex.
             int id = Convert.ToInt32(testGrid.DataKeys[rowIndex].Values[0]);
             int CategoryId = Convert.ToInt32(testGrid.DataKeys[rowIndex].Values[1]);
 
             //here apply your delete logic according to retrieved values
 
         }
      }

Finding a control inside a gridview row

How to find control inside a gridview row?

I have a gridview and inside this view I have a templatefield with server side asp.net button control.

I want to find grid view button control on row and want to stop displaying Button for non admin users.

Solution:

ASPX Code:

<asp:GridView ID="grdUserUpdateHistory" runat="server"  OnRowDataBound="OnRowDataBound" >
               

                <Columns>
 

                    <asp:BoundField HeaderText="UpdateDetail " DataField="UpdateTitle"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="Version" DataField="Version"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="UpdatedDate" DataField="ModifiedDate"></asp:BoundField>
                    
                    <asp:BoundField HeaderText="Modified By " DataField="ModifiedByUser.UserName"></asp:BoundField>
               <asp:TemplateField HeaderText="Delete Users" ShowHeader="True">
                                        <ItemTemplate>
     <asp:Button runat="server" ID="btnDelete " OnClick="BtnDeleteClick" Text="Delete"/>
                                        </ItemTemplate>
     </asp:TemplateField>
                </Columns>
               

            </asp:GridView>

C# code:

Find button control Using OnRowDataBound grid view event:

protected void OnRowDataBound(object Sender, GridViewRowEventArgs E)
        {
                if (E.Row.RowType == DataControlRowType.DataRow)
                {
                    var UserId = grdUserUpdateHistory.DataKeys[E.Row.RowIndex];
                       
                        if (UserId!=0)
                        {
                            var AdminUserEnebled =UserList.Where(U => U.userRole == "Admin");
                                                    
                            var btnDelete = E.Row.FindControl("btnDelete") as Button;
                           
                            if (AdminUserEnebled==null)
                            {
                                btnDelete.Visible = false;
                            }
                        }
                    } 
        }

              

Using Page event Page_load or you can use following logic in any custom method also:

protected void Page_Load(object sender, EventArgs e)
        {
 
            if (!Page.IsPostBack)
            {
 
                grdUserUpdateHistory.DataSource = yourDataSource;
                grdUserUpdateHistory.DataBind();
                for (int i = 0; i < grdUserUpdateHistory.Rows.Count; i++)
                {
                    Button deleteButton= (Button) grdUserUpdateHistory.Rows[i].FindControl("btnDelete");
                }
 
            }
        }